如何在UITableView中获取所有单元格 [英] How to get all cells in a UITableView

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

问题描述

假设我有一个有多行的UITableView。我希望在某个时间点将所有UITableViewCells作为NSArray。我试过了

Let's say I have a UITableView which has multiple rows. I want to get all the UITableViewCells as an NSArray at a certain point of time. I have tried

[tableView visibleCells] 

但这种方法存在问题:我不能拥有当前屏幕中当前不存在的所有单元格。所以我转向另一种方法:

but there is a problem with this approach: I can not have all those cells which are not currently in the current screen. So I turned to another approach:

-(NSArray *)cellsForTableView:(UITableView *)tableView
{
    NSInteger sections = tableView.numberOfSections;
    NSMutableArray *cells = [[NSMutableArray alloc]  init];
    for (int section = 0; section < sections; section++) {
        NSInteger rows =  [tableView numberOfRowsInSection:section];
        for (int row = 0; row < rows; row++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];   // **here, for those cells not in current screen, cell is nil**
            [cells addObject:cell];
        }
    }
    return cells;
}

但似乎这种方法仍然无法获得那些未显示的单元格屏幕。任何人都可以帮我吗?

but seems that this approach still can not get those cells which are not displayed in the screen. Can anyone help me on this?

推荐答案

我认为这不可能,因为tableView不存储所有单元格。它使用缓存机制,该机制仅存储一些不可见的单元以供重用。

I don't think thats possible, simply because the tableView doesn't store all cells. It uses a caching mechanism which only stores some cells, that are not visible, for reuse.

为什么需要所有细胞?也许你可以实现,你正在尝试做什么,否则。

Why do you need all cells? Maybe you can achieve, what you're trying to do, otherwise.

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

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