registerNib UiTableView 在项目损坏后无法正常工作

registerNib UiTableView not working after project corruption

我是 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 硬重置。项目文件都搞砸了。我解决了其他问题。在某些情况下,通过再次添加文件。 但由于某种原因,这段代码现在不起作用。项目中有Cell.xib个文件

错误是'Use of undeclared type Cell'

(cell as? Cell)?.label.text = data[indexPath.row].name

我该如何解决这个问题?

提前致谢!

首先,你只需要注册一次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