从UITableView pagingEnabled获取可见单元格 [英] Getting visible cell from UITableView pagingEnabled

查看:523
本文介绍了从UITableView pagingEnabled获取可见单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有pagingEnabled的UITableView。每个单元占据桌子的观察区域。意思是,每个单元格与表格的高度和宽度相同。我正在使用具有name属性的自定义单元格。我想在标签中显示当前(可查看)单元格的名称。这适用于第一个和最后一个单元格,但中间的任何内容都不是那么容易。问题是为这些中间单元调用了两次cellForRowAtIndexPath。这是从第一个单元格滚动到最后一个单元格然后返回的样子。我已按顺序列出索引,因为cellForRowAtIndexPath会触发该行:

I have a UITableView with pagingEnabled. Each cell takes up the viewing area of the table. Meaning, each cell is the same height and width as the table. I'm using custom cells that have a name property. I'd like to display the name of the current (viewable) cell in a label. This works fine for the first and last cells but anything inbetween isn't so easy. The problem is that cellForRowAtIndexPath is called twice for these middle cells. Here's what it looks like scrolling from the first to last cell and then back. I've listed the indexes in order as cellForRowAtIndexPath fires for that row:

Row             indexPath.row
 0                   0         //view loads and table appears
 1                  1,2        //user scrolls to second cell. cellForRowAtIndexPath fires twice.  First time indexPath.row is one and second time it is two.  This causes cell two's name to display in the label, rather than cell one.
 2                  2,3
 3                   3
 //user starts scrolling back to first cell
 2                  1,2 
 1                  1,0
 0                   0

我可以设置使用NSDate对象来检测我是否在中间行。通过将当前时间与当前时间进行区分,我知道。但是,如果用户在单元格中快速滚动,则可能无法正常工作。有没有其他方法可以做到这一点?

I could set use an NSDate object to detect if I'm in a middle row. By diffing the previous time with current, I'll know. However, if the user scrolls really fast through the cells, that probably doesn't work. Is there another way to do it?

我尝试过使用visiblecells属性的变体但是没有用。 UITableView将加载下一个单元格,即使它不可见,也会导致它成为可见单元格的一部分。

I've tried using variations of visiblecells properties but that didn't work. The UITableView will load the next cell even if it isn't visible, causing it to be part of the visible cells.

推荐答案

好吧,如果您没有找到解决方案,或者接下来遇到这个问题的人,我将为您提供您正在寻找的答案。 UITableView将为您提供您正在寻找的indexPaths,然后UITableView将很乐意为您提供与这些索引路径匹配的单元格:

Well, on the off chance that you never figured out a solution, or for whoever comes to this question next, I'll provide you with the answer you were looking for. UITableView will provide you with the indexPaths you are looking for, and then UITableView will happily provide you with the cells that match those index paths:

UITableView *tableView = self.tableView; // Or however you get your table view
NSArray *paths = [tableView indexPathsForVisibleRows];

//  For getting the cells themselves
NSMutableSet *visibleCells = [[NSMutableSet alloc] init];

for (NSIndexPath *path in paths) {
    [visibleCells addObject:[tableView cellForRowAtIndexPath:path]];
}

// Now visibleCells contains all of the cells you care about.

这篇关于从UITableView pagingEnabled获取可见单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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