0

我试图弄清楚 UITableViewCell 的特定索引是否包含在方法“visibleCells”返回的数组中。我可以使用“indexPathsForVisibleRows”方法,但我想弄清楚如何使用“visibleCells”方法。目前,我所能做的就是获取数组,然后简单地打印出它的内容,如下所示:

NSArray *test = [_table visibleCells];

    for (NSString *i in test) {
        NSLog(@"index is: %@", i);
    }

这给了我以下输出:

index is: <UITableViewCell: 0x715dae0; frame = (0 132; 247 44); text = 'Item 3'; autoresize = W; layer = <CALayer: 0x715d9d0>>
2013-06-13 13:34:19.296 SimpleTable[1203:c07] index is: <UITableViewCell: 0x715e170; frame = (0 176; 247 44); text = 'Item 4'; autoresize = W; layer = <CALayer: 0x715e060>>

我需要有人向我展示如何检查此数组中是否包含特定索引。

4

1 回答 1

0

尝试这个:

if ([[tableView indexPathsForVisibleRows] containsObject:[tableView indexPathForCell:cell]) {
    // the cell is visible
} else {
    // the cell is not visible
}

应该注意的是,执行上述操作存在崩溃风险......containsObject如果您使用 . Using 也将起作用visibleCells

if ([[tableView visibleCells] containsObject:cell]) {
    // the cell is visible
} else {
    // the cell is not visible
}

作为代码的旁注:

NSArray *test = [_table visibleCells];

for (NSString *i in test) {
    NSLog(@"index is: %@", i);
}

for 循环是错误的,因为test它不保存 的实例NSString,它保存UITableViewCell...的实例

于 2013-06-13T18:31:52.283 回答