Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all cells in a UITableView

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?

like image 400
David L Avatar asked Oct 23 '12 10:10

David L


People also ask

How do you clear a Tableview cell?

So, to remove a cell from a table view you first remove it from your data source, then you call deleteRows(at:) on your table view, providing it with an array of index paths that should be zapped. You can create index paths yourself, you just need a section and row number.

What class does UITableView inherit from?

The tableview is the instance of the UITableView class, which inherits the UIScrollView class. We will discuss UIScrollView in the upcoming chapters of this tutorial. In iOS applications, whenever we need to display the single column containing vertically scrolling content, we use tableview.

How do you add a space between UITableView cells?

The way I achieve adding spacing between cells is to make numberOfSections = "Your array count" and make each section contains only one row. And then define headerView and its height. This works great.


1 Answers

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.

like image 103
Tobi Avatar answered Oct 04 '22 16:10

Tobi