TableView reloadData vs. beginUpdates & endUpdates

2020-01-30 05:10发布

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.

3条回答
在下西门庆
2楼-- · 2020-01-30 05:32

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.

查看更多
乱世女痞
3楼-- · 2020-01-30 05:34

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.

查看更多
小情绪 Triste *
4楼-- · 2020-01-30 05:38

The first difference between reloadData and reloadRowsAtIndexPaths is that there are 2 UITableViewCell objects allocated simulteaneosuly for the same indexPath when doing reloadRowsAtIndexPaths (because the tableview 'blends' in the the new cell) . This is sometimes not foreseen by the code in cellForRowAtIndexPath .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 in dequeueReusableCellWithIdentifier when calling reloadRowsAtIndexPaths, instead it returns nil. In contradiction reloadData reuses the cells it already allocated .

The 2nd difference is that endUpdates after reloadRowsAtIndexPaths directly calls cellForRowAtIndexPath (if you set a breakpoint there,endUpdates is visible in the stack trace) whereas reloadData schedules the calls to cellForRowAtIndexPath 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.

查看更多
登录 后发表回答