gpt4 book ai didi

swift - Xcode 表格 View 滚动问题

转载 作者:行者123 更新时间:2023-11-30 11:59:05 24 4
gpt4 key购买 nike

我正在编写一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com