TableView 控制器中的第二个自定义单元格生成错误
Second Custom Cell in TableView Controller Generates Error
我有一个 UITableViewController,我在其中使用了多个自定义单元格。我过去使用过多个自定义单元格没有问题,而且我使用与过去相同的策略来实现它们,所以这个错误让我感到困惑。首先,一些代码:
这就是我在 viewDidLoad 中注册自定义单元格的方式:
// Registering the custom cell
let nib1 = UINib(nibName: "OmniCell", bundle: nil)
tableView.registerNib(nib1, forCellReuseIdentifier: "omniCell")
let nib2 = UINib(nibName: "RankCell", bundle: nil)
tableView.registerNib(nib2, forCellReuseIdentifier: "rankCell")
这就是我在 cellForRowAtIndexPath 中创建它们的方式:
let cell1: OmniCell = self.tableView.dequeueReusableCellWithIdentifier("omniCell") as! OmniCell
let cell2: RankCell = self.tableView.dequeueReusableCellWithIdentifier("rankCell") as! RankCell
当我注释掉与第二个自定义单元格相关的代码时,程序运行正常,但是当同时使用两个自定义单元格时,出现此错误:
this class is not key value coding-compliant for the key rankCell.
两个自定义单元格的实现方式相同,所以我不确定为什么第二个自定义单元格会生成此错误,而第一个自定义单元格不会。我错过了什么?
更新:根据要求,我共享整个 cellforrowatindexpath 函数:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// This line works:
let cell1: OmniCell = self.tableView.dequeueReusableCellWithIdentifier("omniCell") as! OmniCell
// It is this line that breaks if not commented out:
let cell2: RankCell = self.tableView.dequeueReusableCellWithIdentifier("rankCell") as! RankCell
// This line works:
let cell3: ProgressCell = self.tableView.dequeueReusableCellWithIdentifier("progressCell") as! ProgressCell
if indexPath.row == 0 {
return cell1
} else if indexPath.row == 1 {
return cell2
} else {
return cell3
}
}
我设法解决了这个问题。问题是 XIB 中有一个 er运行t 连接。参考这张图:
在一个好的插座上方还有一个 "broken" 插座,例如:
运行kCell ---> 排列单元格
但它不是一个实心圆圈,而是一个细长的符号,太小而无法辨认。我删除了插座和应用程序,并且 运行 正常。我不确定该连接来自何处,none 的其他自定义单元格有一个类似的。
我有一个 UITableViewController,我在其中使用了多个自定义单元格。我过去使用过多个自定义单元格没有问题,而且我使用与过去相同的策略来实现它们,所以这个错误让我感到困惑。首先,一些代码:
这就是我在 viewDidLoad 中注册自定义单元格的方式:
// Registering the custom cell
let nib1 = UINib(nibName: "OmniCell", bundle: nil)
tableView.registerNib(nib1, forCellReuseIdentifier: "omniCell")
let nib2 = UINib(nibName: "RankCell", bundle: nil)
tableView.registerNib(nib2, forCellReuseIdentifier: "rankCell")
这就是我在 cellForRowAtIndexPath 中创建它们的方式:
let cell1: OmniCell = self.tableView.dequeueReusableCellWithIdentifier("omniCell") as! OmniCell
let cell2: RankCell = self.tableView.dequeueReusableCellWithIdentifier("rankCell") as! RankCell
当我注释掉与第二个自定义单元格相关的代码时,程序运行正常,但是当同时使用两个自定义单元格时,出现此错误:
this class is not key value coding-compliant for the key rankCell.
两个自定义单元格的实现方式相同,所以我不确定为什么第二个自定义单元格会生成此错误,而第一个自定义单元格不会。我错过了什么?
更新:根据要求,我共享整个 cellforrowatindexpath 函数:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// This line works:
let cell1: OmniCell = self.tableView.dequeueReusableCellWithIdentifier("omniCell") as! OmniCell
// It is this line that breaks if not commented out:
let cell2: RankCell = self.tableView.dequeueReusableCellWithIdentifier("rankCell") as! RankCell
// This line works:
let cell3: ProgressCell = self.tableView.dequeueReusableCellWithIdentifier("progressCell") as! ProgressCell
if indexPath.row == 0 {
return cell1
} else if indexPath.row == 1 {
return cell2
} else {
return cell3
}
}
我设法解决了这个问题。问题是 XIB 中有一个 er运行t 连接。参考这张图:
在一个好的插座上方还有一个 "broken" 插座,例如: 运行kCell ---> 排列单元格 但它不是一个实心圆圈,而是一个细长的符号,太小而无法辨认。我删除了插座和应用程序,并且 运行 正常。我不确定该连接来自何处,none 的其他自定义单元格有一个类似的。