我是iOS新手。我有一个cellForRowIndexPath实现如下
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("ListCell")
if cell == nil {
let bundle = NSBundle(forClass: self.dynamicType)
tableView.registerNib(UINib(nibName: "Cell",bundle : nil), forCellReuseIdentifier : "ListCell")
cell = tableView.dequeueReusableCellWithIdentifier("ListCell")
}
(cell as? Cell)?.label.text = data[indexPath.row].name
return cell!
}
此代码最初正在运行。我不小心搞了一个git hard reset。项目文件搞砸了。我解决了其他问题。在某些情况下,通过再次添加文件。 但由于某些原因,这段代码现在不起作用。项目中有Cell.xib文件
错误是'使用未声明的类型Cell'
(cell as? Cell)?.label.text = data[indexPath.row].name
我该如何解决这个问题?
提前致谢!
答案 0 :(得分:1)
首先,您只需要注册一次nib,因此请将此代码放入viewDidLoad
或tableView' didSet
:
tableView.registerNib(UINib(nibName: "Cell",bundle : nil), forCellReuseIdentifier : "ListCell")
在cellForRow
中,将代码更改为:
var cell: Cell = tableView.dequeueReusableCellWithIdentifier("ListCell") as! Cell
cell.label.text = data[indexPath.row].name
return cell
您收到错误是因为您未将默认UITableViewCell
投放到自定义Cell