swift - Xcode 表格 View 滚动问题

标签 swift xcode uitableview scroll

我正在编写一个 IOS 应用程序,它将创建一个足球杯。我指定了杯赛的名称和球队数量。然后,当他们点击“继续”按钮时,我会呈现所有团队的表格 View ,并打开多重选择。到目前为止一切都很好。如果团队数量为 2 或 4,则可以正常工作,但如果我指定 8 或更多,则需要在 tableView 上滚动。然后我收到错误 -

teamNames.append((cell?.textLabel?.text)!) - Thread 1 EXC_BREAKPOINT (code=1, subcode=0x10093c624)

我不明白为什么在表格 View 中滚动名称(默认情况下有 7 个团队可见,所以我需要滚动才能到达第 8 个)会导致问题。我认为表格 View 是专门为滚动而设计的。

此外,在控制台区域,我得到:

警告:无法执行支持代码来读取进程中的 Objective-C 类数据。这可能会降低可用类型信息的质量。

这是表格 View 的代码:

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "teamCell", for: indexPath)

    cell.textLabel?.text! = cupTeam [indexPath.row].name!
    return cell
}

感谢您对我的表格 View 问题的任何见解

这是导致错误的代码所在的循环:

   for indexPath in selectedIndexPaths! {
        let cell = teamTable.cellForRow (at: indexPath)
        teamNames.append((cell?.textLabel?.text)!)
    }

最佳答案

selectedIndexPaths 将返回所有选定的索引路径,无论它们是否可见。当你打电话时

let cell = teamTable.cellForRow (at: indexPath)

如果单元格在屏幕上不可见,它将返回 nil。然后你强制打开它以获取值,然后它崩溃了。一个简单的解决方法是使用 if let

for indexPath in selectedIndexPaths! {
    if let cell = teamTable.cellForRow (at: indexPath) {
        teamNames.append((cell.textLabel?.text)!)
    }
}

关于swift - Xcode 表格 View 滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47464672/

相关文章:

ios - 在 uitableview ios 中检索和存储 bool 值

ios - UItableViewCell 内 UIScrollview 中的 UIButton 处理带有数据的事件

ios - UICollectionViewCell 是方形的,但图像是矩形的

ios - 在 sizeForItemAtIndexPath 中调用 UICollectionView Cell

Xcode 遇到意外错误 (0x01c)

ios - 如何在 TAP 上编辑 tableview 单元格而不是滑动 - Swift

Swift Package Manager - 对事件分支的依赖不会拉取新的提交

xcode - 失败的初始化程序'init(fileURL :) cannot override a non-failable initializer

iphone - 我如何在 xcode 4 中引用一个单独的项目?

iphone - 运行 iPhone Simulator 时可以将 Xcode 控制台的日志输出重定向到终端吗?