“Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update” Error

前端 未结 7 1885
無奈伤痛
無奈伤痛 2021-02-13 16:24

I have an app that is selecting a person from their contacts list and takes their First name, last name and email. It then saves the first name to a nsmutablearray and puts it i

相关标签:
7条回答
  • 2021-02-13 16:58

    UITableView must be kept in sync with the data source at all times. Special care must be taken if the data source can change in a background thread.

    When something is added to the data source, call beginUpdate/insert/endUpdate as soon as possible. You don't have to worry about caching these, the UITableView will cache changes to be executed when it determines there is enough cpu time and resources.

    The moment endUpdates is called, the UITable will ask the dataSource for the number of sections and rows again. If your number of sections and row feeds directly from the dataSource, then number sections and rows, plus insertions, minus deletions must equal the numbers returned by the end calls for numberOfSections and numberOfRowsInSection.

    One last tip: avoid mixing calls to 'reloadData' and beginUpdate/endUpdate pairs. Use one or the other, not both.

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