【问题标题】:how can i make headerView scroll (not stay on the top of the tableview ) accompanying with UItableViewCell when i was scrolling tableview当我滚动 tableview 时,如何让 headerView 滚动(不停留在 tableview 的顶部)伴随 UItableViewCell
【发布时间】:2013-12-31 08:24:58
【问题描述】:

在 plainsytle tableview 中,我们通过委托方法为每个部分设置 headerViews

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section 

所以,当我向上滚动 tableview 时,第一部分的 headerView 始终显示在 tableview 的顶部,然后该部分完全消失在 tableview 上,然后第二部分的 headerView 将显示在 tableview 的顶部。所以我的问题是如何让 headerView 滚动伴随 uitableViewCell ,就像组样式 tableview 一样?

【问题讨论】:

  • 您可以通过使用 UITableViewStyleGrouped 样式创建 tableview。

标签: ios uitableview


【解决方案1】:

您可以通过将 table 属性更改为 - 来禁用滚动 sectionHeader

UITableViewStyleGrouped

你必须在 UITableView 的初始化时设置它。

  • 在 StoryBoard 上将表格样式从 Plain 更改为 Grouped

UITableView* table = [[UITableView alloc]initWithFrame:myFrame style:UITableViewStyleGrouped]; 

【讨论】:

    【解决方案2】:

    只需将表格样式从“普通”更改为“分组”即可。

    【讨论】:

      【解决方案3】:

      继承 UITableView 并覆盖此方法

      - (BOOL)allowsHeaderViewsToFloat{
          return NO;
      }
      

      页脚相同

      - (BOOL)allowsFooterViewToFloat{
          return NO;
      }
      

      但我认为这是一个私有 API……您将无法将其提交到 AppStore

      如果您将其上传到 AppStore;那么你还有另外两个选择

      1. 添加普通单元格而不是节标题
      2. 如果你只有一个节,那么你可以简单地使用表头而不是节头

      【讨论】:

      • 我把它放在一个类别而不是子类中,它通过了审查\m/。
      【解决方案4】:

      我遇到了同样的问题,当我浏览时,我来到了这个链接 See The accepted answer,我得出的结论是你需要将 tableViewHeader 样式设置为 Group,它可以工作 Charm。

      【讨论】:

      • 漂亮而简单,不需要子类化:D
      【解决方案5】:

      你可以改变tableView的样式。

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

      【讨论】:

        【解决方案6】:

        如果您有多个部分,您可以使用一些第三方 API 对其进行自定义

        否则,您可以使用 Grouped table 而不是普通的 tableView 尝试此操作。

        当您将自定义视图分配为 tableView 时尝试这样 标题视图它成为表格的标题视图,它将随着表格视图滚动

         -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
            {
                UIView *sectionView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
        
                tableView.tableHeaderView =sectionView;
        
                return sectionView;
            }
        

        【讨论】:

          【解决方案7】:

          将表格的UITableViewStyleUITableViewStylePlain更改为UITableViewStyleGrouped,这将使标题与单元格一起向上滚动。

          【讨论】:

            【解决方案8】:

            斯威夫特:

            正如 Hani Ibrahim 指出的,您需要继承 UITableView。苹果说你: must specify style at creation.

            所以:

            override init(frame: CGRect, style: UITableViewStyle) {
                    super.init(frame: frame, style: .grouped)
            

            注意我的 super.init 如何使用 .grouped 作为样式。 对于这种风格的 tableview (.grouped),Apple 还说:

            Summary
            
            A table view whose sections present distinct groups of rows. The section headers and footers do not float.
            
            

            然后你可以调用以下 tableView 委托方法: viewForHeaderInSectionheightForHeaderInSection (请注意,这些仅用于页眉,如果您也需要页脚,请使用页脚对应项)

            func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                    let headerLabel = UILabel()
                    headerLabel.text = "My cool header title"
                    headerLabel.textAlignment = .center
                    headerLabel.font = UIFont(name: "OpenSans-Bold", size: 16)
                    return headerLabel
            }
            
            func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
                    return 44
                }
            

            应该这样做!祝你好运。

            【讨论】:

            • 或者让tableView = UITableView(frame: .zero, style: .grouped)
            【解决方案9】:

            通过更改 Storyboard 的属性来禁用滚动部分标题

            故事板

            1. 属性检查器
            2. 风格
            3. 从下拉菜单中将样式从普通更改为分组

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-02-01
              • 1970-01-01
              • 2016-01-16
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多