【发布时间】:2014-08-29 13:39:16
【问题描述】:
我有一个表格视图,其中包含创建为 .xib 的自定义单元格。我没有使用故事板。我有一个问题,我无法用来自 web 服务结果的数据填充我的表。另外,我在自定义单元格中有 4 个标签。在我的自定义单元格类中,当我尝试为每个项目设置标签时,它给了我上面的致命错误。
这是我的代码:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
...
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell: ItemManagementTVCell = tableView?.dequeueReusableCellWithIdentifier("cell") as ItemManagementTVCell
if let ip = indexPath
{
let item: Item = self.itemList[indexPath.row] as Item
cell.setCell(item.itemName, status: item.itemStatus, duration: item.itemDuration, price: item.itemPrice)
}
return cell
}
}
我的自定义单元类在这里:
导入 UIKit
class ItemManagementTVCell: UITableViewCell {
@IBOutlet var lblItemName: UILabel!
@IBOutlet var lblItemPrice: UILabel!
@IBOutlet var lblItemDuration: UILabel!
@IBOutlet var lblItemStatus: UILabel!
override func awakeFromNib()
{
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool)
{
super.setSelected(selected, animated: animated)
}
func setCell(name: String, status: Int, duration: Int, price: Int)
{
self.lblItemName.text = name
self.lblItemStatus.text = String(status)
self.lblItemDuration.text = "Duration: \(String(duration)) months"
self.lblItemPrice.text = String(price) + " $"
}
}
我在“setCell”方法块中遇到错误。
我已经阅读了很多问题和解决方案,我尝试了所有这些对我不起作用。
感谢您的回答,
最好的问候。
解决方案:我已经通过将单元格项目链接到单元格自己的而不是链接到文件所有者来解决了这个问题。我的问题已经解决了。
【问题讨论】:
-
你能在这里添加解决方案吗?
-
这很简单,我将逐步解释它 1-) 单击您的单元格 2-) 单击位于右侧“隐藏或显示实用程序”菜单中的连接检查器。 3-) 单击文件的所有者,如果有任何连接的项目,将其全部删除。然后单击单元格视图并在那里连接您的单元格项目。注意:我没有使用故事板。此解决方案适用于 xibs。 @neosergio
-
感谢您的澄清
-
我遇到了类似的问题,这段代码得到“致命错误,意外发现为零”。但这很疯狂,因为如果我插入... print(indexPath) print(tableView) 我可以清楚地看到两者都不是零。更糟糕的是,这是一个间歇性问题。有时有效,有时无效。