ios - 无法使用 dequeueReusableCell(withIdentifier : for:) from a xib file

标签 ios swift tableview

dequeueReusableCell(withIdentifier: for:) 步骤中,我无法将我的表格 View 单元格转换为自定义表格 View 单元格。

我的代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldcell", for: indexPath) as! TextFieldTableViewCell
    cell.labelTitle.text = array[indexPath.row]
    return cell
}

我已经在 viewDidLoad() 中注册了我的单元格:

tableView.register(TextFieldTableViewCell.self, forCellReuseIdentifier: "TextFieldcell")

我收到一条错误消息:

fatal error :在展开可选值时意外发现 nil

我敢打赌它没有通过转换,这就是单元格为空的原因。任何人都可以看到错误吗?

谢谢!

更新代码

 override func viewDidLoad() {
        super.viewDidLoad()
        let bundle = Bundle(for: TextFieldTableViewCell.self)
        let nib = UINib(nibName: "MyTableViewCell", bundle: bundle)
        tableView.register(nib, forCellReuseIdentifier: "TextFieldcell")
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldcell", for: indexPath) as! TextFieldTableViewCell
        cell.labelTitle.text = array[indexPath.row]
        return cell
    }
}

显然问题仍然存在,澄清一下,这是我创建这个自定义 UITableViewCell 所采取的步骤

  1. 创建一个 .swift 文件和一个 .xib 文件
  2. 在 .xib 文件中,我删除了样板代码并添加了两个 UILabel
  3. 我将 .xib 文件的文件所有者设置为我的 swift,它有我的两个 IBOutlets
  4. 接下来,在我要实例化自定义 tableViewCell 的 tableViewController 中,在 viewDidLoad() 方法中,我添加了以下内容:

let bundle = Bundle(for: TextFieldTableViewCell.self)

让 nib = UINib(nibName: "MyTableViewCell", bundle: bundle)

tableView.register(nib, forCellReuseIdentifier: "TextFieldcell")

我创建这段代码的原因如下:因为我已经使用 .xib 文件和界面生成器创建了我的自定义表格 View 单元格,所以我需要使用 tableView.register(nibName: forCellReuseIdentifier:) 函数。

但是,当我运行代码时,我收到一条错误消息:reason: 'Could not load NIB in bundle: 'NSBundle

这是我的自定义单元格的代码:

class TextFieldTableViewCell: UITableViewCell {

    @IBOutlet weak var labelTitle: UILabel!
    @IBOutlet weak var userInput: UITextField!
}

谁能看出问题出在哪里?谢谢!

最佳答案

您需要注册您的 nib 对象。

在 viewDidLoad() 中:

let nib = UINib(nibName: "nameOfYourNibFile", bundle: nil)

tableView.register(nib, forCellReuseIdentifier: "yourIdentifier")

关于ios - 无法使用 dequeueReusableCell(withIdentifier : for:) from a xib file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45665751/

相关文章:

ios - 如何 "restart"iOS 应用程序/从 UIApplicationDelegate 实例重新实例化初始 View Controller ?

ios - 在 Swift 中引用一个按钮

swift - 较新版本的 swift 的 textkit 不可用

ios - UITableView 中的 TextField 退出FirstResponder

ios - -[__NSArrayI addObject :]: unrecognized selector sent to instance

ios - AFNetworking 3.1.0 和调度组

ios - 在 ios 中使用后台

swift - iOS13导航栏后退栏按钮item tint颜色

ios - Swift - IBACTION 如何执行 segue

ios - 子类 uitableview 单元格以快速更改分隔符高度