Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TableView reloadData vs. beginUpdates & endUpdates

I got a tricky problem regarding updating my TableView, i get different results using different methods of updating it, let me explain:

Situation 1: I use [tbl reloadData]; where tbl is my TableView, to update the TableView - works as intended.

Situation 2: I use:

[tbl beginUpdates]; [tbl reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight]; [tbl endUpdates]; 

Where tbl is my TableView, and indexPaths is an array containing all the indexPaths present in the TableView. Now the array is fine, it contains all the correct indexPaths (double and triple checked) but for some reason - this does not work as intended.

Now I realize that this is an X-Y problem (where I ask for Y but my problem is really X because I think solving Y will solve X) and thats only because I feel it's a bit complicated explaining X (the consequence of said above problem) in an easy way, so I'd rather refrain from that if possible.

So, down to my question: Is there a difference between the two ways of updating the TableView (aside from the animation bit of course) or should I suspect the problem to lay elsewhere?

EDIT: Okay, I'll try to explain what the symptoms are:

In the cellForRowAtIndexPath-method I add a button to each cell with an assigned tag which is equal to the cell's indexPath row, like such:

btn.tag = indexPath.row; 

The reason I do this is so I can identify each button as they all call the same function:

- (void)btnPressed:(id)sender 

When I then update the cells - because some values in the cells have changed - Situation 1 makes everything work fine, Situation 2 however - mixes up the tags so the next time one of the buttons are pressed, they no longer have the correct tags.

The mix-up does appear random to me, but the randomization occurs differently depending on which cells button I press first. I hope this clarifies my problem.

like image 655
Tobias Tovedal Avatar asked Nov 17 '11 20:11

Tobias Tovedal


People also ask

What does TableView beginUpdates do?

beginUpdates()Begins a series of method calls that insert, delete, or select rows and sections of the table view.

What does Tableview reloadData do?

reloadData()Reloads the rows and sections of the table view.


1 Answers

From the UITableView documentation

beginUpdates
Begin a series of method calls that insert, delete, or select rows and sections of the receiver.

That means, you should not use this unless you are inserting, deleting or selecting. You are doing neither of these.

Also, you should end beginUpdates with endUpdates, not reloadData. Documentation:

This group of methods must conclude with an invocation of endUpdates.

like image 99
Mundi Avatar answered Sep 25 '22 06:09

Mundi