自定义UITableViewCell是空白的(Swift)

4
我将尝试在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"


1
你在身份检查器下将xib的类设置为自定义单元格类了吗? - NSGangster
你已经添加了行中的章节数吗? - Link Openheim
1个回答

7

不要注册UITableViewCell类,而应该注册nib文件,例如:

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接