UITableView - 如何在用户滚动时保持表行固定 [英] UITableView - How to keep table rows fixed as user scrolls

查看:102
本文介绍了UITableView - 如何在用户滚动时保持表行固定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to be able to fix the position of certain rows in a UITableView as the user scrolls.

具体来说,我有一个表,某些行是后面的行的标题,我希望当用户向上滚动时标题保持在屏幕的顶部。当用户滚动到足以使下一个标题行取代它时,它将会移开。

Specifically, I have a table whereby certain rows are "headers" for the rows that follow, and I'd like the header to stay at the top of the screen as the user scrolls up. It would then move out of the way when the user scrolls far enough that the next header row would take its place.

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

A similar example would be the Any.DO app. The "Today", "Tommorrow" and "Later" table rows are always visible on the screen.

有没有人对如何实现这一点有任何建议?

Does anyone have any suggestions about how this could be implemented?

我正在考虑遵循TableDidScroll委托并将我自己的单元格放在表格视图前面的适当位置。问题是,在其他时候,我真的希望这些单元格成为真正的表格单元格,以便它们可以由用户重新排序。

I'm currently thinking of follow the TableDidScroll delegate and positioning my own cell in the appropriate place in front of the table view. The problem is that at other times I'd really like these cells to be real table cells so that they can be, for example, reordered by the user.

谢谢,

Tim

推荐答案

我一直在玩这个和我想出了一个简单的解决方案。

I've been playing about with this and I've come up with a simple solution.

首先,我们向控制器添加一个UITableViewCell属性。这应该初始化,使其看起来与我们用于创建false节标题的行单元格完全相同。

First, we add a single UITableViewCell property to the controller. This should be initialize such that looks exactly like the row cells that we'll use to create the false section headers.

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

Next, we intercept scrolling of the table view

- (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];
}

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

Finally, we need to intercept actions on that cell and do the right thing...

这篇关于UITableView - 如何在用户滚动时保持表行固定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆