您可以使用章节进行操作。如果我正确理解你的问题,你想要做的是,你必须在屏幕中间显示标题,同时在标题上方有一些单元格和标题下面的一些单元格,当你滚动tableView时,应该滚动标题到顶部,当它到达顶部时,您希望头部保持在顶部,而单元格应在头部下方滚动。
因此,要做到这一点回报两条从数据源
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 2
}
,并在数据源返回条件细胞
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>
}
}
然后重写委托
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
}
}
设置条件身高为
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
if(section == 0) {
return 0
} else {
return <Return your header view height>
}
}
我们在这里做的是,我们在第二部分显示头部有一些单元格,所以你会在tableView中看到一些没有头部的单元格,在这些单元格下面你会看到headerView和一些单元格,当你滚动你的tableView headerView将滚动单元格直到它到达顶部。
希望这会帮助你。
请分享到目前为止你做了什么(以及如何)。 – mobiuseng