如何使标题单元格与Swift 2.0中的tableview单元一起移动

Blu*_*Sky 12 xcode uitableview ios swift

我正在尝试创建一个tableview,它在tableview中间启动标题,然后可以使用添加的tableView Cells向上滚动到顶部然后停止,然后tableview单元格可以只在其下滚动.我得到的标题位于tableview的中间,但tableView Cells只是在它上面滚动,标题并没有真正滚动到顶部.它只是停留在原地.我希望能够在滚动列表时让标题移动,然后在它到达列表顶部时停止,然后它只是之后移动的tableView单元格.

我使用的是最新版本的XCode

Dev*_*del 41

转到故事板>选择视图>选择TABLEVIEW>属性检查器> CHNAGE STYPE到PLAIN的GROUPPED

见图: 在此输入图像描述

  • 我没有对你进行投票,但这是一个糟糕答案的例子.首先,您的说明全部大写并且有多个拼写错误.但更重要的是,您的说明并没有描述读者期望它们发挥作用的原因.什么是关于改变标题行为的分组样式? (2认同)

Bha*_*odi 1

您可以使用部分来完成此操作。如果我正确理解你的问题,你想要做的是,你必须在屏幕中间显示标题,同时在标题上方有一些单元格,在标题下方有一些单元格,当你向上滚动 tableView 时,你的标题应该滚动到顶部,当它到达顶部时,您希望标题保持在顶部,而单元格应滚动到标题下方。

因此,要执行此操作,请从数据源返回两个部分

func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
     return 2
}
Run Code Online (Sandbox Code Playgroud)

并在数据源中返回条件单元格

func tableView(tableView: UITableView, numberOfRowsInSection section:    Int) -> Int
{
    If(section == 1) {
      return <Return number of cell you want to display above header>
    } else {
      return <Return number of cell you want to display below header>
    }
}
Run Code Online (Sandbox Code Playgroud)

然后覆盖委托

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
    if(section == 0) {
        return nil //As you do not have to display header in 0th section, return nil
    } else {
        return <Return your header view>

       //You can design your header view inside tableView as you design cell normally, set identifier to it and dequeue cell/headerView as:
       //However this may result in unexpected behavior since this is not how the tableview expects you to use the cells.

        let reuseIdentifier : String!
    reuseIdentifier = String(format: "HeaderCell")

        let headerView = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: NSIndexPath(forRow: 0, inSection: 0))

        return headerView
    }
}
Run Code Online (Sandbox Code Playgroud)

将条件高度设置为

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
    if(section == 0) {
        return 0 
    } else {
        return <Return your header view height>
    }
}
Run Code Online (Sandbox Code Playgroud)

我们在这里所做的是,我们在第二部分显示标题,其中会有一些单元格,因此您将在 tableView 中看到一些没有标题的单元格,在这些单元格下方,您将看到带有一些单元格的 headerView,当您向上滚动 tableView 时将随着单元格滚动直到到达顶部。

希望对你有帮助。