Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash with Missing cell for newly visible row when updating UITableView

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

like image 245
iOSGeek Avatar asked Nov 01 '16 10:11

iOSGeek


3 Answers

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.

like image 67
emrekyv Avatar answered Sep 19 '22 21:09

emrekyv


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..)

like image 38
Andrey Zhukov Avatar answered Sep 19 '22 21:09

Andrey Zhukov


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)
}
like image 20
Mohammed Avatar answered Sep 19 '22 21:09

Mohammed