gpt4 book ai didi

ios - swift : thread 1 exc_bad_instruction (code=exc_i386_invop subcode=0x0) in tableview

转载 作者:搜寻专家 更新时间:2023-11-01 06:44:41 26 4
gpt4 key购买 nike

我正在使用 Tableview Controller 制作两个原型(prototype)单元格,应用程序因

而崩溃

thread 1 exc_bad_instruction (code=exc_i386_invop subcode=0x0) pointed to >>> return cell! in the first cell

fatal error: unexpectedly found nil while unwrapping an Optional value

   override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 10
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (indexPath.section == 0) {
let cell = tableView.dequeueReusableCellWithIdentifier("one", forIndexPath: indexPath) as? one
cell?.textLabel?.text = "book"
return cell!
} else {

let cell = tableView.dequeueReusableCellWithIdentifier("two", forIndexPath: indexPath) as! two

cell.textLabel?.text = "not a book"
return cell

}

enter image description here

最佳答案

如果我可以改进代码:

 let cellID = (indexPath.section == 0) ? "one" : "two"
if let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath)
as? UITableViewCell
cell.textLabel!.text = (cellID == "one") ? "book" : "not a book"
return cell
}
else {
assertionFailure("Unable to dequeue a cell of ID \(cellID)")
return nil
}

然后您将获得合理的错误行为。我敢打赌,即使您已将您的命名为onetwo,您也忽略了在其中设置它们的“标识符”每个单元格原型(prototype)的属性检查器中的 StoryBoard。

您的回答 非常错误。您不应该创建大量表格单元格。这是对表格的滥用。您应该只使用重用队列中的那些。

使用我的代码版本,并在 if let cell 之后设置一个断点。在调试器中分析对象,看看它到底是什么类型。

如果相反,您遇到了 assertionFailure,您仍然没有在 StoryBoard 中正确设置标识符。向我们展示屏幕截图以说服我们。

关于ios - swift : thread 1 exc_bad_instruction (code=exc_i386_invop subcode=0x0) in tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31689494/

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