【问题标题】:Is it possible to disable floating headers in UITableView with UITableViewStylePlain?是否可以使用 UITableViewStylePlain 禁用 UITableView 中的浮动标题?
【发布时间】:2010-11-07 14:57:18
【问题描述】:

我正在使用UITableView 来布局内容“页面”。我正在使用表格视图的标题来布局某些图像等,如果它们不浮动但保持静态,就像样式设置为 UITableViewStyleGrouped 时一样,我更喜欢它。

然后使用UITableViewStyleGrouped,有没有办法做到这一点?我想避免使用分组,因为它会在我的所有单元格中添加一个边距,并且需要禁用每个单元格的背景视图。我想完全控制我的布局。理想情况下,它们是“UITableViewStyleBareBones”,但我在文档中没有看到该选项...

非常感谢,

【问题讨论】:

标签: ios objective-c iphone cocoa-touch uikit


【解决方案1】:

以下两种情况的最佳解决方案:

场景1:如果tableView样式不是.grouped。将其更改为分组没有问题。页脚不会浮动。

场景 2:如果您只想在 tableView 的末尾添加一个页脚。它的风格是.plain

步骤:

  1. 在末尾再添加一节。
  2. 确保在添加的部分中(nuberOfRowsInSection 方法)行数为零。
  3. 向该部分添加自定义页脚。
  4. 将 heightForFooterInSection 设置为您的自定义页脚,并将 .zero 设置为其他部分的页脚。

示例:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

//注意:过滤您要添加自定义部分的部分

    let footerView = UIView()
    submitOrganiser?.showFooterAtTheEnd(view: footerView) //my method to customise the footer, use your implementation
    return footerView
}

【讨论】:

    【解决方案2】:

    **Swift 5.3 |以编程方式**

    private func buildTableView() -> UITableView {
        let tableView = UITableView()
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.rowHeight = UITableView.automaticDimension
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = .none
        let dummyViewHeight: CGFloat = 80
        tableView.tableFooterView = UIView(
            frame: CGRect(x: .zero,
                          y: .zero,
                          width: tableView.bounds.size.width,
                          height: dummyViewHeight))
    
        tableView.contentInset = UIEdgeInsets(top: .zero, left: .zero, bottom: -dummyViewHeight, right: .zero)
        return tableView
    }
    

    【讨论】:

      【解决方案3】:

      实现这一点的一种可能更简单的方法:

      目标-C:

      CGFloat dummyViewHeight = 40;
      UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, dummyViewHeight)];
      self.tableView.tableHeaderView = dummyView;
      self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 0, 0, 0);
      

      斯威夫特:

      let dummyViewHeight = CGFloat(40)
      self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: dummyViewHeight))
      self.tableView.contentInset = UIEdgeInsets(top: -dummyViewHeight, left: 0, bottom: 0, right: 0)
      

      部分标题现在将像任何常规单元格一样滚动。

      【讨论】:

      • 这该死的作品就像一个魅力!表格标题视图标题的存在阻止了部分标题浮动。并且带有适当内容插图的表格的不可见标题可以解决问题..非常感谢!想知道为什么这个答案没有得到太多关注
      • 还是不信!没有私有 API,没有深入研究类,什么都没有!我多年来一直面临这个问题,并且一直在滚动视图之上添加表格视图(禁用表格视图滚动),这将使表格标题正常滚动。但刚刚开始搜索苹果最近是否提供了任何新方法来解决这个问题。很高兴我偶然发现了你的答案。再次感谢
      • 我看到的唯一缺点是顶部标题将不再显示(您只能通过向上滚动查看)。至少对我来说没有显示
      • 您仍然可以显示表格的 tableHeaderView,方法是将其顶部的高度增加一个等于节标题高度的数字,并将内容插入减少相同的数字。
      • 我希望我能再次投票,我很确定这至少是我第三次回答这个问题,这解决了我的问题。
      【解决方案4】:

      一个棘手的方法是为标题添加一个空白部分。因为 section 没有单元格,所以它根本不会浮动。

      【讨论】:

        【解决方案5】:

        sn-p 只为第一部分显示一个粘性标题。其他部分标题将与单元格一起浮动。

        func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        
            if section == 1 {
                tableView.contentInset = .zero
            }
        
        }
        
        func tableView(_ tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int) {
            if section == 0 {
                tableView.contentInset = .init(top: -tableView.sectionHeaderHeight, left: 0, bottom: 0, right: 0)
            }
        }
        

        【讨论】:

          【解决方案6】:

          @samvermette 解决方案的变体:

          /// Allows for disabling scrolling headers in plain-styled tableviews
          extension UITableView {
          
              static let shouldScrollSectionHeadersDummyViewHeight = CGFloat(40)
          
              var shouldScrollSectionHeaders: Bool {
                  set {
                      if newValue {
                          tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: bounds.size.width, height: UITableView.shouldScrollSectionHeadersDummyViewHeight))
                          contentInset = UIEdgeInsets(top: -UITableView.shouldScrollSectionHeadersDummyViewHeight, left: 0, bottom: 0, right: 0)
                      } else {
                          tableHeaderView = nil
                          contentInset = .zero
                      }
                  }
          
                  get {
                      return tableHeaderView != nil && contentInset.top == UITableView.shouldScrollSectionHeadersDummyViewHeight
                  }
              }
          
          }
          

          【讨论】:

            【解决方案7】:

            更改您的 TableView 样式:

            self.tableview = [[UITableView alloc] initwithFrame:frame style:UITableViewStyleGrouped];
            

            根据苹果的 UITableView 文档:

            UITableViewStylePlain- 一个普通的表格视图。任何节标题或 页脚显示为内联分隔符并在表格时浮动 视图被滚动。

            UITableViewStyleGrouped - 一个表格视图,其部分呈现不同 行组。部分页眉和页脚不浮动。

            【讨论】:

            • ☝️ 这应该被标记为正确答案。此外,对于情节提要爱好者,当您选择 UITableView 时,您在情节提要属性检查器中拥有此属性
            【解决方案8】:

            (对于由于错误的表格样式而来到这里的人)通过属性检查器或通过代码将表格样式从普通更改为分组:

            let tableView = UITableView(frame: .zero, style: .grouped)
            

            【讨论】:

            • "其他然后使用 UITableViewStyleGrouped,有没有办法做到这一点?"
            • 好在我学会了在实施最受好评的答案之前检查所有答案:)
            • 现在这个家伙,就是你要找的答案。请注意,您也可以在 AttributesInspector 中找到 TableView Style 属性。
            • 该问题特别提到寻找一种方式“其他然后使用 UITableViewStyleGrouped”
            • 很好的答案!但是,分组表视图对于每个部分的第一个和最后一个单元格有不同的分隔符,这可能是您不喜欢的。
            【解决方案9】:

            参考:https://stackoverflow.com/a/26306212

            let tableView = UITableView(frame: .zero, style: .grouped)

            加号

            func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
                if section == 0 {
                    return 40
                }else{
                    tableView.sectionHeaderHeight = 0
                }
            
                return 0
            }
            

            这有助于使用标题空间

            【讨论】:

            • 请不要那样做?
            【解决方案10】:

            根据@samvermette的回答,我在Swift中实现了上面的代码,方便程序员使用swift。

            let dummyViewHeight = CGFloat(40)
            self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: dummyViewHeight))
            self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 0, 0, 0)
            

            【讨论】:

              【解决方案11】:

              适用于 Swift 3+

              只需使用UITableViewStyleGrouped 并将页脚的高度设置为零:

              override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
                  return .leastNormalMagnitude
              }
              

              【讨论】:

              • 您可以返回 0 而不是 .leastNormalMagnitude。你还需要实现tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
              • @IliasKarim 将其设置为 0 使其具有非零高度。另外,在这种情况下也不需要实现viewForFooterInSection 函数。
              • 0.leastNormalMagnitude 具有相同的效果。实现viewForFooterSection 方法是必要的。你应该返回零。这是在带有 Xcode 11.3.1 的 Swift 5.1 上。
              【解决方案12】:

              也许你可以在 tableView 上使用scrollViewDidScroll 并根据当前可见的标题更改contentInset

              这似乎适用于我的用例!

              extension ViewController : UIScrollViewDelegate{
              
                  func scrollViewDidScroll(_ scrollView: UIScrollView) {
                      guard   let tableView = scrollView as? UITableView,
                          let visible = tableView.indexPathsForVisibleRows,
                          let first = visible.first else {
                          return
                      }
              
                      let headerHeight = tableView.rectForHeader(inSection: first.section).size.height
                      let offset =  max(min(0, -tableView.contentOffset.y), -headerHeight)
                      self.tableView.contentInset = UIEdgeInsetsMake(offset, 0, -offset, 0)
                 }
              }
              

              【讨论】:

              • 非常感谢你的朋友,你的回答满足了我目前所需要的,祝你好运
              【解决方案13】:

              获得所需内容的最简单方法是将表格样式设置为UITableViewStyleGrouped, 分隔符样式为UITableViewCellSeparatorStyleNone:

              - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
                  return CGFLOAT_MIN; // return 0.01f; would work same 
              }
              
              - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
                  return [[UIView alloc] initWithFrame:CGRectZero];
              }
              

              不要尝试将页脚视图返回为nil,不要忘记设置页眉高度和页眉视图,之后您必须获得所需的内容。

              【讨论】:

                【解决方案14】:

                也许您可以简单地使标题视图背景透明:

                - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
                    view.tintColor = [UIColor clearColor];
                }
                

                或全局应用:

                    [UITableViewHeaderFooterView appearance].tintColor = [UIColor clearColor];
                

                【讨论】:

                  【解决方案15】:

                  警告:此解决方案实现了一个保留的 API 方法。这可能会阻止应用被 Apple 批准在 AppStore 上分发。

                  我已经描述了在my blog中浮动部分标题的私有方法

                  基本上,您只需要继承UITableView 并在它的两个方法中返回NO

                  - (BOOL)allowsHeaderViewsToFloat;
                  - (BOOL)allowsFooterViewsToFloat;
                  

                  【讨论】:

                  • 一个答案不应该仅仅因为它引用私有 API 而被否决(不是每个人都在编写要提交到 AppStore 的代码)。当答案恰好是正确的时,这是双重正确的。为这些方法返回 NO 的子类化(或更容易地,创建 UITableView 类别)会阻止标题浮动。如果您希望将此添加为公共 API,请提交错误报告:bugreport.apple.com
                  • 如果我们在 App 中实现该功能,Apple 将如何意识到您正在使用私有 API?据他们所知,我只是在UITableView的一个子类中实现了一个方法,如果不反汇编代码他们甚至都看不到,而且不会那么容易。它不是在私有 API 中调用一个方法,它只是实现一个......
                  • @jemmons 如果您不想让我对您的答案投反对票,请将其标记为“此处使用私有 API”。免责声明:)
                  • 您的“我的博客”链接已损坏
                  • @jemmons “不是每个人都在编写要提交到 AppStore 的代码” Appstore 并不是真正的问题。问题是苹果可以更改私有 API 而无需沟通或弃用这会破坏您的应用程序。这就是为什么你不应该使用私有 API 而不是被 Apple 拒绝的一个更好的理由
                  【解决方案16】:

                  您应该能够通过使用自定义单元格来制作标题行来伪造这一点。然后,它们将像表格视图中的任何其他单元格一样滚动。

                  您只需要在 cellForRowAtIndexPath 中添加一些逻辑,以便在它是标题行时返回正确的单元格类型。

                  您可能必须自己管理您的部分,即将所有内容放在一个部分中并伪造标题。 (您也可以尝试为标题视图返回一个隐藏视图,但我不知道这是否可行)

                  【讨论】:

                  • 是的……不过,这似乎有点骇人听闻。这似乎是 Apple 的疏忽,因为显然代码是用来做这件事的,我们只需要能够设置一个标志。
                  • 在下面查看我的答案,以获取无需任何特殊技巧即可实现效果的正确方法,只需将每个部分的第一个单元格作为其部分标题管理!
                  【解决方案17】:

                  还有另一种棘手的方法。主要思想是将节号加倍,第一个只显示 headerView 而第二个显示真实的单元格。

                  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
                      return sectionCount * 2;
                  }
                  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
                      if (section%2 == 0) {
                          return 0;
                      }
                      return _rowCount;
                  }
                  

                  接下来需要做的是实现 headerInSection 委托:

                  - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
                      if (section%2 == 0) {
                          //return headerview;
                      }
                      return nil;
                  }
                  
                  - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
                      if (section%2 == 0) {
                          //return headerheight;
                      }
                      return 0;
                  }
                  

                  这种方法对您的数据源也几乎没有影响:

                  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                      int real_section = (int)indexPath.section / 2;
                      //your code
                  }
                  

                  与其他方法相比,这种方式是安全的,同时不改变tableview的frame或contentInsets。 希望这可能会有所帮助。

                  【讨论】:

                    【解决方案18】:

                    您可以在上面添加一个Section(零行),然后将上面的sectionFooterView设置为当前section的headerView,footerView不浮动。 希望对大家有所帮助。

                    【讨论】:

                    • FooterViews 会浮动,就像 HeaderViews 一样。它们只是漂浮在屏幕底部,而不是顶部。
                    【解决方案19】:

                    在您的界面生成器中单击您的问题表视图

                    然后导航到 Attributes Inspector 并将 Style Plain 更改为 Grouped ;) 简单

                    【讨论】:

                      【解决方案20】:

                      我还有另一个更简单的解决方案,可以在没有自动布局的情况下使用,并且通过 XIB 完成所有操作:

                      1/ 通过将标题直接拖放到 tableview 上,将标题放在 tableview 中。

                      2/ 在新建标题的 Size Inspector 中,只需更改其自动调整大小:您应该只保留顶部、左侧和右侧的锚点,以及水平填充。

                      这应该可以解决问题!

                      【讨论】:

                        【解决方案21】:

                        另一种方法是在您想要标题的部分之前创建一个空白部分,然后将您的标题放在该部分上。因为该部分是空的,所以标题会立即滚动。

                        【讨论】:

                          【解决方案22】:

                          要完全删除浮动部分的标题部分,您可以这样做:

                          - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
                          {
                              return [[UIView alloc] init];
                          }
                          

                          return nil 不起作用。

                          要禁用浮动但仍显示部分标题,您可以提供具有自己行为的自定义视图。

                          【讨论】:

                            【解决方案23】:

                            好的,我知道已经晚了,但我必须这样做。 到目前为止,我已经花了 10 个小时寻找可行的解决方案,但没有找到完整的答案。确实发现了一些提示,但初学者很难理解。所以我不得不投入我的 2 美分并完成答案。

                            正如少数答案中所建议的那样,我能够实现的唯一可行的解​​决方案是在表格视图中插入普通单元格并将它们作为节标题处理,但实现它的更好方法是插入这些单元格位于每个部分的第 0 行。这样我们就可以非常轻松地处理这些自定义的非浮动标题。

                            所以,步骤是。

                            1. 使用样式 UITableViewStylePlain 实现 UITableView。

                              -(void) loadView
                              {
                                  [super loadView];
                              
                                  UITableView *tblView =[[UITableView alloc] initWithFrame:CGRectMake(0, frame.origin.y, frame.size.width, frame.size.height-44-61-frame.origin.y) style:UITableViewStylePlain];
                                  tblView.delegate=self;
                                  tblView.dataSource=self;
                                  tblView.tag=2;
                                  tblView.backgroundColor=[UIColor clearColor];
                                  tblView.separatorStyle = UITableViewCellSeparatorStyleNone;
                              }
                              
                            2. 像往常一样实现 titleForHeaderInSection(您可以使用自己的逻辑来获取此值,但我更喜欢使用标准委托)。

                              - (NSString *)tableView: (UITableView *)tableView titleForHeaderInSection:(NSInteger)section
                              {
                                  NSString *headerTitle = [sectionArray objectAtIndex:section];
                                  return headerTitle;
                              }
                              
                            3. 像往常一样实现 numberOfSectionsInTableView

                              - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
                              {
                                  int sectionCount = [sectionArray count];
                                  return sectionCount;
                              }
                              
                            4. 像往常一样实现 numberOfRowsInSection。

                              - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
                              {
                                  int rowCount = [[cellArray objectAtIndex:section] count];
                                  return rowCount +1; //+1 for the extra row which we will fake for the Section Header
                              }
                              
                            5. 在 heightForHeaderInSection 中返回 0.0f。

                              - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
                              {
                                  return 0.0f;
                              }
                              
                            6. 不要实现 viewForHeaderInSection。完全删除该方法,而不是返回 nil。

                            7. 在 heightForRowAtIndexPath 中。检查 if(indexpath.row == 0) 并返回节标题所需的单元格高度,否则返回单元格的高度。

                              - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
                              {
                                  if(indexPath.row == 0)
                                  {
                                      return 80; //Height for the section header
                                  }
                                  else
                                  {
                                      return 70; //Height for the normal cell
                                  }
                              }
                              
                            8. 现在在 cellForRowAtIndexPath 中,检查 if(indexpath.row == 0) 并按照您希望的部分标题实现单元格并将选择样式设置为无。 ELSE 按照你想要的正常单元格来实现单元格。

                              - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
                              {
                                  if (indexPath.row == 0)
                                  {
                                      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SectionCell"];
                                      if (cell == nil)
                                      {
                                          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SectionCell"] autorelease];
                                          cell.selectionStyle = UITableViewCellSelectionStyleNone; //So that the section header does not appear selected
                              
                                          cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SectionHeaderBackground"]];
                                      }
                              
                                      cell.textLabel.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:indexPath.section];
                              
                                      return cell;
                                  }
                                  else
                                  {
                                      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
                              
                                      if (cell == nil) 
                                      {
                                          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
                                          cell.selectionStyle = UITableViewCellSelectionStyleGray; //So that the normal cell looks selected
                              
                                          cell.backgroundView =[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellBackground"]]autorelease];
                                          cell.selectedBackgroundView=[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectedCellBackground"]] autorelease];
                                      }
                              
                                      cell.textLabel.text = [[cellArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row -1]; //row -1 to compensate for the extra header row
                              
                                      return cell;
                                  }
                              }
                              
                            9. 现在实现 willSelectRowAtIndexPath 并在 indexpath.row == 0 时返回 nil。这将确保 didSelectRowAtIndexPath 永远不会为 Section 标题行触发。

                              - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
                              {
                                  if (indexPath.row == 0)
                                  {
                                      return nil;
                                  }
                              
                                  return indexPath;
                              }
                              
                            10. 最后在 didSelectRowAtIndexPath 中,检查 if(indexpath.row != 0) 并继续。

                              - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
                              {
                                  if (indexPath.row != 0)
                                  {
                                      int row = indexPath.row -1; //Now use 'row' in place of indexPath.row
                              
                                      //Do what ever you want the selection to perform
                                  }
                              }
                              

                            这样你就完成了。您现在有一个完美滚动的非浮动部分标题。

                            【讨论】:

                            • 实现了'willSelectRowAtIndexPath',这样控件在选择Section Header行时永远不会到达'didSelectRowAtIndexPath'。
                            • 实际上,我在选择标题时折叠+展开行。但很高兴知道您可以做到这一点
                            • 很好的答案,但为什么在 'didSelectRowAtIndexPath' 中检查 'if (indexPath.row != 0)'?只是为了更加安全吗?使用您的“willSelectRowAtIndexPath”实现,该行永远不能为 0,对吗?
                            • @Glenn85,抱歉回复晚了。是的,这只是为了增加安全性。
                            【解决方案24】:

                            在思考如何解决这个问题时,我想起了关于 UITableViewStyleGrouped 的一个非常重要的细节。 UITableView 实现分组样式(单元格周围的圆形边框)的方式是向 UITableViewCells 添加自定义 backgroundView,而不是向 UITableView。每个单元格都根据其在部分中的位置添加一个背景视图(上面的行获取部分边框的上部,中间的行获取侧边框,底部的获取 - 好吧,底部)。 因此,如果我们只想要一个简单的样式,并且我们的单元格没有自定义背景视图(90% 的情况都是这样),那么我们需要做的就是使用 UITableViewStyleGrouped,并删除自定义背景.这可以通过以下两个步骤来完成:

                            将我们的 tableView 样式更改为 UITableViewStyleGrouped 在我们返回单元格之前,将以下行添加到 cellForRow:

                            cell.backgroundView=[[[UIView alloc] initWithFrame:cell.bounds] autorelease];

                            就是这样。 tableView 样式将变得与 UITableViewStylePlain 完全一样,除了浮动标题。

                            希望这会有所帮助!

                            【讨论】:

                              【解决方案25】:

                              查看如何使用 StoryBoard 实现标题的答案:Table Header Views in StoryBoards

                              还要注意,如果你不实施

                              viewForHeaderInSection:(NSInteger)section
                              

                              它不会浮动,这正是你想要的。

                              【讨论】:

                                【解决方案26】:

                                这可以通过在 UITableViewController 的 viewDidLoad 方法中手动分配标题视图来实现,而不是使用委托的 viewForHeaderInSection 和 heightForHeaderInSection。例如,在您的 UITableViewController 子类中,您可以执行以下操作:

                                - (void)viewDidLoad {
                                    [super viewDidLoad];
                                
                                    UILabel *headerView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 40)];
                                    [headerView setBackgroundColor:[UIColor magentaColor]];
                                    [headerView setTextAlignment:NSTextAlignmentCenter];
                                    [headerView setText:@"Hello World"];
                                    [[self tableView] setTableHeaderView:headerView];
                                }
                                

                                当用户滚动时,标题视图将消失。我不知道为什么会这样,但它似乎实现了你想要做的。

                                【讨论】:

                                • 这不是一个部分,这是顶部的 1 个标题。
                                【解决方案27】:

                                虽然这可能无法解决您的问题,但当我想做类似的事情时,它确实对我有用。我没有设置页眉,而是使用了上面部分的页脚。拯救我的是这个部分很小而且本质上是静态的,所以它永远不会滚动到视图底部以下。

                                【讨论】:

                                • 好主意。我喜欢这个 hack。
                                • 有点好笑。欣赏这个想法,但黑客就是黑客。标题会在底部开始浮动,因此并不能真正解决问题
                                【解决方案28】:

                                UITableViewStyleGrouped 的有趣之处在于 tableView 将样式添加到 cells 而不是 TableView。

                                样式作为 backgroundView 添加到单元格中,作为一个名为 UIGroupTableViewCellBackground 的类,它根据单元格在该部分中的位置处理绘制不同的背景。

                                所以一个非常简单的解决方案就是使用UITableViewStyleGrouped,将表格的backgroundColor设置为clearColor,然后简单的替换cellForRow中单元格的backgroundView:

                                cell.backgroundView = [[[UIView alloc] initWithFrame:cell.bounds] autorelease];
                                

                                【讨论】:

                                • 这应该是最佳答案。这里有一篇关于它的博文:corecocoa.wordpress.com/2011/09/17/…
                                • 这个解决方案真的不起作用。我一直在周围的牢房周围留出额外的空间。有什么想法吗?
                                • 单元格的contentView不还是要右移吗?
                                【解决方案29】:

                                忽略 XAK。如果您希望您的应用有机会被苹果接受,请不要探索任何私有方法。

                                如果您使用的是 Interface Builder,这是最简单的。您将在视图顶部(图像所在的位置)添加一个 UIView,然后在其下方添加您的表格视图。 IB 应该相应地调整它的大小;也就是说,tableview 的顶部接触到您刚刚添加的 UIView 的底部,并且它的高度覆盖了屏幕的其余部分。

                                这里的想法是,如果那个 UIView 实际上不是 table view 的一部分,它就不会随着 tableview 滚动。即忽略 tableview 标题。

                                如果您不使用界面生成器,它会稍微复杂一些,因为您必须为 tableview 设置正确的位置和高度。

                                【讨论】:

                                • 这不能回答问题。 Tricky 指的是位于单元格之间的视图 titleHeaderViews,而不是您似乎建议的表头视图。
                                【解决方案30】:

                                您可以通过在 tableview 委托类中实现 viewForHeaderInSection 方法轻松实现此目的。此方法需要一个 UIView 作为返回对象(这是您的标题视图)。我在我的代码中做了同样的事情

                                【讨论】:

                                • 原始海报不想要自定义标题。他们想要一个不会“浮动”在表格顶部的标题。您的建议仍然存在。
                                • 用viewforheader把tableview改成UITableViewStyleGrouped,然后让它停止
                                猜你喜欢
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 2011-06-07
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 2014-07-21
                                • 1970-01-01
                                相关资源
                                最近更新 更多