XCode 6不识别第二个tableViewCell


let cell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as MessageTableViewCell
let cell2 = tableView!.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as MessageTableViewCell

第一个单元格被很好地识别。第二种则不然。我有一个故事板,为每个原型单元设置了适当的标识符。(动态)tableView有两个单元格在它上面,这应该是好的,从我的理解。下面是我收到的错误:

*由于未捕获的异常而终止应用程序'NSInternalInconsistencyException',原因:'无法取出单元格。标识符为Cell2的必须注册笔尖或类标识符或连接storyboard中的原型单元格

如果第二个原型单元格的标识符不是"Cell2",那么这将是完全有意义的。

这是整个函数中的代码,注意我甚至没有返回单元格2,只是简单地将其脱离队列:

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
    let cell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as MessageTableViewCell
    let cell2 = tableView!.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as MessageTableViewCell
if UIApplication.sharedApplication().statusBarOrientation.isLandscape == true {
    cell.messageLabel.preferredMaxLayoutWidth = cell.frame.size.width - 80
} else {
    cell.messageLabel.preferredMaxLayoutWidth = cell.frame.size.width - 35
}
preferredWidth = cell.messageLabel.preferredMaxLayoutWidth
cell.messageLabel.text = friends[indexPath!.row]
cell.messageLabel.sizeToFit()
cell.messageLabel.setNeedsDisplay()
return cell
}

它在第二行(函数内部)崩溃。

注意:我现在使用XCode 6 Beta 4

我也有过这样的经历。试试这个:

tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")

我创建了一个新的笔尖,在那里重新创建了单元格,并插入了以下代码,所有这些结合起来解决了问题。

var nibName = UINib(nibName: "MainTableViewCell", bundle:nil)
self.tableView.registerNib(nibName, forCellReuseIdentifier: "SecondCell")

最新更新