Daniele B Daniele B - 1 year ago 502
iOS Question

iOS/Swift: tableView cellForRowAtIndexPath crash

I am experiencing a EXC_BREAKPOINT crash at line 389

386 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
387 let cell = tableView.dequeueReusableCellWithIdentifier("StationsCell", forIndexPath: indexPath)
388 cell.textLabel?.text = self.suggestionStations?[indexPath.row]
389 return cell
390 }

here is the crash report from Crashlytics, happening at 2% of users:

Thread : Crashed: com.apple.main-thread
0 Trenìt! 0x10004a2e4 specialized MasterViewController.tableView(UITableView, cellForRowAtIndexPath : NSIndexPath) -> UITableViewCell (MasterViewController.swift:389)
1 Trenìt! 0x100046488 @objc MasterViewController.tableView(UITableView, cellForRowAtIndexPath : NSIndexPath) -> UITableViewCell (MasterViewController.swift)
2 UIKit 0x18c6b239c -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 544
3 UIKit 0x18c6a6fc4 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2360
4 UIKit 0x18c49cc60 -[UITableView layoutSubviews] + 172
5 UIKit 0x18c3b9874 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 572
6 QuartzCore 0x18bd11d58 -[CALayer layoutSublayers] + 168
7 QuartzCore 0x18bd0c944 CA::Layer::layout_if_needed(CA::Transaction*) + 320

any idea why this happens?

Answer Source

The crash was caused by the suggestionStations array not having a value at the index.

I was not calling ReloadData() on the table when the array was reset to 0. So the the table function was still called in a race condition when the value at the index didn't exist anymore.

The fix was to call ReloadData() also after resetting the array. Otherwise there could have been a check that the index was defined for the array, but this shouldn't be needed if reloadData is properly called each time.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download