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.
From the
UITableView
documentationThat means, you should not use this unless you are inserting, deleting or selecting. You are doing neither of these.
Also, you should end
beginUpdates
withendUpdates
, notreloadData
. Documentation:Call this method if you want subsequent insertions, deletion, and selection operations (for example, cellForRowAtIndexPath: and indexPathsForVisibleRows) to be animated simultaneously.
I think this is what you want. beginUpdates & endUpdates can change the UItableview with animation.
The first difference between
reloadData
andreloadRowsAtIndexPaths
is that there are 2UITableViewCell
objects allocated simulteaneosuly for the same indexPath when doingreloadRowsAtIndexPaths
(because the tableview 'blends' in the the new cell) . This is sometimes not foreseen by the code incellForRowAtIndexPath
.The surprise comes from the fact that even if a cell was already allocated for a particular cell identfier the table view does not give you back this cell indequeueReusableCellWithIdentifier
when callingreloadRowsAtIndexPaths
, instead it returns nil. In contradictionreloadData
reuses the cells it already allocated .The 2nd difference is that
endUpdates
afterreloadRowsAtIndexPaths
directly callscellForRowAtIndexPath
(if you set a breakpoint there,endUpdates
is visible in the stack trace) whereasreloadData
schedules the calls tocellForRowAtIndexPath
at a later time (not visible in the stack trace).However you would need to post a bit more code to give us insight what you are doing there. In principle the indexPaths of the new cells are identical to the old ones also with
reloadRowsAtIndexPaths
as long as you don't delete or insert rows.