UITableView syntax for calling cellForRowAtIndexPath: for all cells

I am having problems with UITableView reloadData . I found that it only calls cellForRowAtIndexPath if new cells are added or removed.

Example: I have five cells, each of which contains five rows. If I add a new cell and call reloadData , the table will be updated and I will see it. However, if I enter one of the five cells, add a new row, then go back and call reloadData , none of the table view delegate methods will be called.

My question is: is it possible to force a table view to completely reload data in all visible cells?

+6
source share
6 answers

I found a problem - I had my cell setup code in the if(cell == nil) block, so since the cells were recycled, they did not change. Due to the fact that my setup code from this block fixed the problem.

+9
source

I found that the reboot partitions are loading data again. If you have only one section, you can try:

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];

+1
source

You can try this

 [tableView reloadData]; 

or

 [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade]; 

put this in the commitEditingStyle method.

+1
source

Well, in the table view, only cellForRowAtIndexPath will be displayed on the visible cells, but if you call reloadData , it will make something happen here, and I'm not sure what it is.

0
source

Even if your table view has 50 rows, there will be as many cells as can be seen at a time. This is the story behind reuse. So to force "all cells" does not exist. If a new cell appears, the data is loaded dynamically.

The only way to change the cell is to change the data that is passed by the dataSource cellForRowAtIndexPath method.

0
source

Do not extract the code from the if (cell == nil) block if (cell == nil) . Instead, create a representative identifier for the cell you are creating; try and make sure that all the contents of the cell are indicated in the identifier. For example, if you have 3 numbers, make sure that these three numbers in the identifier in a unique way apply only to a cell with such content.

Let's say you have three NSArray properties in your class: array1, array2, and array3, which have int values ​​wrapped inside NSNumber objects. You want to use these NSArrays to populate a UITableView, this is what I would do:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier = [NSString stringWithFormat:@"%@-%@-%@", [[array1 objectAtIndex:indexPath.row] intValue], [[array2 objectAtIndex:indexPath.row] intValue], [[array3 objectAtIndex:indexPath.row] intValue]]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; //Build your cell here. } return cell; } 
0
source

Source: https://habr.com/ru/post/906557/


All Articles