iOS/Swift:tableView cellForRowAtIndexPath 崩溃

Dan*_*e B 4 uitableview ios swift

我在第 389 行遇到 EXC_BREAKPOINT 崩溃

\n\n
386    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {\n387        let cell = tableView.dequeueReusableCellWithIdentifier("StationsCell", forIndexPath: indexPath)\n388        cell.textLabel?.text = self.suggestionStations?[indexPath.row]\n389        return cell\n390    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

以下是 Crashlytics 的崩溃报告,发生在 2% 的用户身上:

\n\n

Thread : Crashed: com.apple.main-thread\n0 Treni\xcc\x80t! 0x10004a2e4 specialized MasterViewController.tableView(UITableView, cellForRowAtIndexPath : NSIndexPath) -> UITableViewCell (MasterViewController.swift:389)\n1 Treni\xcc\x80t! 0x100046488 @objc MasterViewController.tableView(UITableView, cellForRowAtIndexPath : NSIndexPath) -> UITableViewCell (MasterViewController.swift)\n2 UIKit 0x18c6b239c -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 544\n3 UIKit 0x18c6a6fc4 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2360\n4 UIKit 0x18c49cc60 -[UITableView layoutSubviews] + 172\n5 UIKit 0x18c3b9874 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 572\n6 QuartzCore 0x18bd11d58 -[CALayer layoutSublayers] + 168\n7 QuartzCore 0x18bd0c944 CA::Layer::layout_if_needed(CA::Transaction*) + 320\n

\n\n

知道为什么会发生这种情况吗?

\n

Dan*_*e B 5

崩溃是由于suggestionStations数组索引处没有值引起的。

当数组重置为 0 时,我没有调用ReloadData()表。因此,当索引处的值不再存在时,表函数仍然在竞争条件下被调用。

修复方法是在重置数组后也调用 ReloadData()。否则,可能会检查是否为数组定义了索引,但如果每次都正确调用 reloadData,则不需要这样做。