ios - 自定义 UITableViewCell 为空白(Swift)

标签 ios swift uitableview

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

我已将我的手机注册到:

self.myTableView.registerClass(customCell.self, forCellReuseIdentifier: "cell")

我还设置了以下内容:

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
}

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

最佳答案

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

self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")

nibName 是 XIB 文件的名称,但没有 .xib 扩展名。

关于ios - 自定义 UITableViewCell 为空白(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38543016/

相关文章:

iphone - 带有底部工具栏的 UI 导航 Controller 在 View 切换时丢失 UIBarButtonItem

ios - 如何绘制CALayer 的线路径模拟橡皮擦效果?

ios - 如何创建SnapChat TableView效果

ios - 在带有 xib 文件的 UIView 中使用 CollectionView

ios - 如何以编程方式在父容器中制作两个相同大小的 UIView

swift - UITableviewCell : set constraints programmatically inside ContentView using autoresize

uitableview - -[UITableView _addScrollViewScrollObserver :] while using large title navigation ios 11 中的断言失败

iphone - UITableView 滚动时重复单元格

ios - 在调用 `deleteRowsAtIndexPaths:withRowAnimation` 之前删除对象仍然生成 : Invalid update: invalid number of rows in section 0

ios - 将索引路径发送到 Firebase(如按钮)