TableView reloadData vs. beginUpdates & endUpdates

前端 未结 3 1008
礼貌的吻别
礼貌的吻别 2020-12-04 18:22

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

Situation 1:

相关标签:
3条回答
  • 2020-12-04 18:31

    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.

    0 讨论(0)
  • 2020-12-04 18:41

    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.

    0 讨论(0)
  • 2020-12-04 18:42

    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.

    0 讨论(0)
提交回复
热议问题