自定义 UITableViewCell 为空(Swift)

Mat*_*ler 4 uitableview ios swift

我正在尝试在 Swift 中设置自定义 UITableViewCell。我还创建了 XIB 文件和 swift 文件。但是,当我运行应用程序时,单元格不会填充我的自定义单元格,而是一个完全空白的单元格。

我已将我的手机注册到:

self.myTableView.registerClass(customCell.self, forCellReuseIdentifier: "cell")
Run Code Online (Sandbox Code Playgroud)

我还设置了以下内容:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return 3
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   let cell = myTableView.dequeueReusableCellWithIdentifier("cell") as! customCell
   cell.descriptionLabel.text = "test"
   return cell
}
Run Code Online (Sandbox Code Playgroud)

此外,我还在自定义单元格中设置了一个指向 UILabel 的链接。当我尝试更新它时,应用程序崩溃了。该线cell.descriptionLabel.text = "test"如上图所示。

pab*_*ros 7

UITableViewCell您必须注册笔尖,而不是注册类,例如:

self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
Run Code Online (Sandbox Code Playgroud)

nibName是 XIB 文件的名称,但不带.xib扩展名。