0

现在我正在获取可见 tableview 单元格的索引路径。但是我想从可见行中获取 3 个索引路径,我该怎么做?

NSArray *visiblePaths = [tblView indexPathsForVisibleRows];
4

1 回答 1

1

假设只有一个部分,您可以创建它们,注意该部分的最大行数:

NSMutableArray *visiblePaths = [tblView indexPathsForVisibleRows] mutableCopy];
NSInteger lastIndexPath = [visiblePaths lastObject];
NSInteger lastRow = lastIndexPath.row;
NSInteger extraRows = 3;
NSInteger maxRow = MIN(lastRow+extraRows, [self tableView:tblView numberOfRowsInSection:0] - 1);

for (int i = lastRow+1; i < maxRow; i++) {
    NSIndexPath *newPath = [NSIndexPath indexPathForRow:i inSection:0];
    [visiblePaths addObject:newPath];
}

它也适用于更多部分(和/或第一个可见之前的行)。希望很清楚如何扩展它以涵盖这些要求。

于 2013-03-12T14:25:36.813 回答