iOS / Swift:tableView cellForRowAtIndexPath崩溃
我在389行遇到EXC_BREAKPOINT崩溃
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 }
这里是来自Crashlytics的崩溃报告,发生在2%的用户:
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
任何想法为什么发生这种情况
这个崩溃是由suggestionStations
数组在索引处没有值造成的。
当数组被重置为0时,我没有在表上调用ReloadData()
。因此,当索引值不再存在时,表函数仍然在竞争条件中被调用。
解决的办法是在重置数组后重新调用ReloadData()。 否则,可能会检查索引是否为数组定义,但是如果每次都正确调用reloadData,则不需要这样做。
我们有同样的问题。 这个问题在iOS 10 beta1上可以重现100%。
虽然很难相信,但我们设法通过在故事板上提供非常短的单元格标识符来解决这个问题。 像“PhoneContactCell”重命名为“PCell”。
经过一番调查,我们认为这是一个故事板的bug:如果一个单元格的原型被复制和修改,那么它可能会有这个问题。
确保你已经确定你的单元格标识符为“StationsCell”在你的Main.Storyboard
。 select单元格并转到属性检查器。
还要在函数之前加上override
,所以应该是override func
。