19

I am experiencing a crash when I delete a row.

// Updating my data model 
....
// apply the updates
self.tableView.beginUpdates()
self.tableView.deleteRows(at: indexPathsToDelete, with: .automatic)
self.tableView.endUpdates()

Steps to reproduce - Add rows - Delete rows, specifically making sure there's some rows outside the current screen (that will then be in screen when the deletion is successful - Repeat until crash occurs

It doesn't always happen so my best guess is that it will happen only when the cells it's trying to load get recycled

This is in 10.0 simulator with Xcode 8.0

  *** Assertion failure in -[UITableView _updateWithItems:updateSupport:],
 /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3599.6/UITableView.m:3149
 Missing cell for newly visible row 2
 (null)

code for cellForRowAt

if isMultipe{
   let cell =  tableView.dequeueReusableCell(withIdentifier: DetailsTableViewCell.defaultIdentifier, for: indexPath) as! DetailsTableViewCell
return cell
    } else {
let cell =  tableView.dequeueReusableCell(withIdentifier: DetailsMultipleTableViewCell.defaultIdentifier, for: indexPath) as! DetailsMultipleTableViewCell
   return cell
}

the same bug reported here : https://forums.developer.apple.com/thread/49676

8
  • How do you dequeue your cell? Can you show your code in cellForRow... ?
    – Duyen-Hoa
    Nov 1, 2016 at 10:54
  • @Hoa question updated
    – iOSGeek
    Nov 1, 2016 at 11:18
  • So you registered the .xib to the cell class and dequeue it when need. There is maybe a problem with iOS10, but try to check if your cell is nilor not after the dequeueReusableCell line.
    – Duyen-Hoa
    Nov 1, 2016 at 11:23
  • @Hoa the cells are not nil because it would crash with the force unwrap I guess
    – iOSGeek
    Nov 1, 2016 at 11:58
  • your are removing object from array and adding new object in array so you need to reloadTableview Nov 7, 2016 at 12:51

7 Answers 7

8

I had the same problem and I found out that UITableView has serious problems with animating insert/update/delete rows when section headers are variable height. Just convert the height to some constant and try it again.

This actually means deleting estimatedSectionHeaderHeight.

3
  • But heightForHeaderInSection is ok to have? Or do you have to delete that too. Apr 5, 2017 at 19:17
  • @tettoffensive heightForHeaderInSection is fine to have.
    – emrekyv
    Apr 5, 2017 at 21:12
  • 2
    looks that even without estimatedSectionHeaderHeight you can still get missing cell for newly visible row ##
    – Mobigital
    Oct 23, 2017 at 23:32
1

In my case it was crashing when cells have used autolayout for calculating it's height. So, the only guaranteed solution is use manual calculating heights. (what is actually sad..)

2
  • That's what I have end up doing, but there should be an explanation
    – iOSGeek
    Dec 9, 2016 at 15:52
  • Did you use autolayout for the cell itself? I already turned off autolayout for calculating heights, but I'm still using autolayout for positioning the subviews. I'm still getting the error though. May 15, 2017 at 2:05
1

In my case I had this issue just for inserting the first row to an empty section, so I tried to fix it like this:

self.array.append(item)
if self.array.count > 1 {
    self.tableView.beginUpdates()
    self.tableView.insertRows(at: indexPathes, with: .top)
    self.tableView.endUpdates()
} else {
    self.tableView.reloadSections([1], with: .fade)
}
1

This problem happened to me when I had cells with different sizes. My code:

tableView.estimatedRowHeight = 150
tableView.rowHeight = UITableViewAutomaticDimension

During moving/inserting/deleting, app was crashing with "Missing cell for newly visible row" message. So I removed this line:

tableView.estimatedRowHeight = 150

and implement delegate method in which returned separate estimated height for each row.

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {}

Hope it will help someone.

0

Remove indexPath in tableView.dequeueReusableCell

if isMultipe{
   let cell =  tableView.dequeueReusableCell(withIdentifier: DetailsTableViewCell.defaultIdentifier) as! DetailsTableViewCell
return cell
    } else {
let cell =  tableView.dequeueReusableCell(withIdentifier: DetailsMultipleTableViewCell.defaultIdentifier) as! DetailsMultipleTableViewCell
   return cell
}
1
  • I already have dequeueReusableCell without indexPath and still hitting this error.....
    – Mobigital
    Oct 23, 2017 at 20:17
0

I had a similar problem.

emrekyv suggested to delete estimatedSectionHeaderHeight but I have a table with one seaction and no header and no footer.

Expanding his idea and setting estimatedRowHeight to 0 (= not automatic) fixed the problem.

So the rule is:

Do not estimate any height when updating a UITableView.

0

In my case this happened when I forgot to call super.prepareForReuse() in my custom subclass.

See https://developer.apple.com/documentation/uikit/uitableviewcell/1623223-prepareforreuse:

If you override this method, you must be sure to invoke the superclass implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.