reloadRowsAtIndexPaths:withRowAnimation: crashes my app

后端 未结 8 1994
谎友^
谎友^ 2021-02-07 01:33

I got a strange problem with my UITableView: I use reloadRowsAtIndexPaths:withRowAnimation: to reload some specific rows, but the app crashes with an seemingly unre

8条回答
  •  广开言路
    2021-02-07 01:53

    The problem is that you, probably, have changed size of your table. For example, you have add or remove some source data for table view.

    In that case, when you call reloadData your table is completely reloaded including sizes of sections and number of that sections.

    But when you call reloadRowsAtIndexPaths:withRowAnimation: that parameters of your table view are not reloaded. That causes next problem: when you are trying to reload some cell table check the size of table view and sees that it has been changed. That results in a crash. This method can be used only when you wan't to reload view of the cell (for example, label has changed or you want to change its size).

    Now if you want to remove/add cells from/to table view you should use next approach:

    1. Inform table that its size will be changed by calling method beginUpdates of UITableView
    2. Inform about inserting new row(s) using method - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    3. Inform about removing row(s) using method - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    4. Inform table that its size has been changed by calling method endUpdates of UITableView

    Hope it will be helpful

提交回复
热议问题