IOS TableView operation principle and data refresh pit

IOS TableView operation principle and data refresh pit
scene sketch.Png
The scene is like this:
  1. 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
  2. In the detail page, refresh dataList (the number of elements has not changed), and notify the home page reloadData refresh UI
  3. 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
    , viewDidLoad,
    , viewWillAppear:
  • Execute the following method multiple times before you load the data source:
    , numberOfSectionsInTableView:,
    , numberOfRowsInSection:
  • The view displays the
    viewDidAppear: on the screen

2. after loading, the data source starts to refresh

  • Refresh
    reloadData
  • Perform the following methods multiple times:
    , numberOfSectionsInTableView:,
    , numberOfRowsInSection:,
    , heightForRowAtIndexPath:
  • 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, &lt, the number of rows that are held by the page, the number of times executed, the number of elements, the number of elements, &gt, 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,
    viewWillAppear:
  • Key points, pit points,
    , cellForRowAtIndexPath:

Note: here is the push –&gt 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.

IOS TableView operation principle and data refresh pit
solution example.Png