UITableView – 如何保持表格行固定为用户滚动

我希望能够在用户滚动时修复UITableView中某些行的位置。

具体来说,我有一个表,其中某些行是后面的行的“标题”,我希望标题留在屏幕的顶部,随着用户向上滚动。 然后,当用户滚动到足够长的距离,以便下一个标题行取代时,它就会移开。

Any.DO应用程序也是一个类似的例子。 屏幕上始终显示“今天”,“明天”和“稍后”的表格行。

有没有人有任何build议如何可以实施?

我目前正在考虑遵循TableDidScroll委托,并将我自己的单元格放置在表视图前面的适当位置。 问题是,在其他时间,我真的很喜欢这些细胞是真正的表格单元格,以便它们可以,例如,由用户重新sorting。

谢谢,

蒂姆

我一直在玩这个,我想出了一个简单的解决scheme。

首先,我们将一个UITableViewCell属性添加到控制器。 这应该初始化,看起来就像我们将用来创build错误节标题的行单元格。

接下来,我们拦截表视图的滚动

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { // Add some logic here to determine the section header. For example, use // indexPathsForVisibleRows to get the visible index paths, from which you // should be able to get the table view row that corresponds to the current // section header. How this works will be implementation dependent. // // If the current section header has changed since the pervious scroll request // (because a new one should now be at the top of the screen) then you should // update the contents. IndexPath *indexPathOfCurrentHeaderCell = ... // Depends on implementation UITableViewCell *headerCell = [self.tableView cellForRowAtIndexPath:indexPathOfCurrentHeaderCell]; // If it exists then it's on screen. Hide our false header if (headerCell) self.cellHeader.hidden = true; // If it doesn't exist (not on screen) or if it's partially scrolled off the top, // position our false header at the top of the screen if (!headerCell || headerCell.frame.origin.y < self.tableView.contentOffset.y ) { self.cellHeader.hidden = NO; self.cellHeader.frame = CGRectMake(0, self.tableView.contentOffset.y, self.cellHeader.frame.size.width, self.cellHeader.frame.size.height); } // Make sure it's on top of all other cells [self.tableView bringSubviewToFront:self.cellHeader]; } 

最后,我们需要拦截在该单元格上的操作,并做正确的事情…

这是普通UITableView实例中节标题的默认行为。 如果你想创build一个自定义头,在你的表视图委托中实现tableView:viewForHeaderInSection:方法并返回你的头的视图。

虽然您将不得不pipe理部分和行而不是仅行。