The scene is like this:
- The APP home page is a UITableView list, the data source is set to global variable dataList, and then the second page details page is clicked
- In the detail page, refresh dataList (the number of elements has not changed), and notify the home page reloadData refresh UI
- DataList an element and then delete, and notify the home page to refresh the list of reloadData, lead to a crash and report to
Terminating app due to uncaught exception’NSRangeException’reason: -[__NSArrayM objectAtIndex:]’: index 0 beyond bounds for empty array’error
This bug is put forward after the test, the author studied for a long time, seemingly normal refresh tableView list the logic of what could go wrong, and finally found out that there is no deep understanding of the operation mechanism of UITableView controlled price, this is for everyone to share, to avoid repeated drop pit.
1. UITableView view loading logic
- Load view
- Execute the following method multiple times before you load the data source:
- The view displays the
viewDidAppear: on the screen
2. after loading, the data source starts to refresh
- Perform the following methods multiple times:
- Execute the corresponding number
cellForRowAtIndexPath: based on the number of data source elements
Because of the reuse pool, there are 2 cases: the number of elements, <, the number of rows that are held by the page, the number of times executed, the number of elements, the number of elements, >, the number of rows that the page contains, the number of times executed, the number of rows that the page contains
3. after the details page refreshes the data source and list, return to the home page
- The view will appear on the screen,
- Key points, pit points,
Note: here is the push –> home page; details page in the Navi stack: if the details page does not have a data source and a list of page tableView list refresh, then pop back to the home page, page list is not self refresh (i.e. reloadData manual refresh) if the details page has passed the agreement or notification callbacks to the home page tableView list data source and list refresh, then pop back to the home page, page list will start to bypass numberOfRowsInSection: and cellForRowAtIndexPath: into the method of execution, then according to the notification reloaData manual refresh. At this point, the default home page list is executed according to the data source element before push. The details page has already removed an element from the data source. At this point, the data source has changed, and the array cross boundary problem will naturally arise.
4. solving problems
In fact, as long as you understand the tableView list control in the process of a series of callback methods, operation principles and logic, the above problems will be solved.