【问题标题】:Why does my app crash on "indexPath.row" load?为什么我的应用程序在“indexPath.row”加载时崩溃?
【发布时间】:2024-01-04 17:46:03
【问题描述】:

我可能没有提供足够的信息来正确提出这个问题,但我试图找出为什么我的代码在加载到 indexPath.row 时会崩溃。

大部分代码被截断,这是我的 TableViewController.swift

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return minions.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return minionCellAtIndexPath(indexPath)
}

    func minionCellAtIndexPath(indexPath:NSIndexPath) -> MinionCell {

        let minion = minions[indexPath.row]

        //Set the cell to be the custom, reusable, MinionCell
        let cell = tableView.dequeueReusableCellWithIdentifier(minionCellIdentifier, forIndexPath: indexPath) as! MinionCell

        if let name = minion.name {

            cell.nameLabel?.text = minion.name

        } else {

            cell.nameLabel?.text = "No data available."

        }

        return cell
    }

据我所知,let minion = minions[indexPath.row] 抛出错误,错误发生在 __Thread 1__ 上,语法显示:

函数签名特化 Arg[0] = 拥有保证

控制台输出似乎没有显示错误可能是什么,尽管我怀疑这是否与我从 Swift 1.2 过渡到 Swift 2 有关。

【问题讨论】:

  • 您是否尝试过检查 minions 数组是否在该行之前填充?可能是表数据当时还没有准备好。我认为如果您在某处发布完整的测试用例以便人们可以自己运行它,您可能会获得更多帮助。只需使用说明问题的 tableview 创建一个测试项目。然后放到github或者类似的地方。

标签: swift uitableview swift2 nsindexpath


【解决方案1】:

问题应该出在 dequeueReusableCellWithIdentifier 上,确保它在故事板中正确设置。为您的单元格提供标识符名称的最佳位置是您的 minioncell 类。

【讨论】:

    【解决方案2】:

    我最终通过修改解决了这个问题;

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { return minionCellAtIndexPath(indexPath) }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    通过更改 return 函数并将我的所有表格单元格配置添加到此函数中,我发现正确填充表格完全成功。

    【讨论】:

      最近更新 更多