iOS/swift : tableView cellForRowAtIndexPath crash

标签 ios swift uitableview

我在第 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% 的用户身上:

线程:崩溃:com.apple.main-thread 0 Trenìt! 0x10004a2e4 专用 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/swift : tableView cellForRowAtIndexPath crash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36671222/

相关文章:

iphone - 在 UITableviewcell 中存储 boolean 值

iphone - 在游戏中调用 addObject 时出现 IOS 8 XCode 6 beta 4 异常

ios - 如何将数组元素放在表格 View 单元格上

ios - 不要删除 UITable 的第一行

iphone - 在 uitableview 中禁用弹跳也会禁用 iOS 5 上的滚动,但不会禁用 iOS4

iphone - 隐藏注释副标题,或更改颜色

ios - 快速3中的dispatch_semaphore_t

ios - 代码;如何从另一个 ViewController 调用 AppDelegate 中的函数

ios - OAuthSwift (1) 连接

ios - 在 UIView 上应用渐变未获得预期结果