我有两个带有两个xib单元的表格视图。如何在第二个表格中注册第二个xib单元格?
它一直放在第一个表的单元格中。
这是我的代码:
let cellNib = UINib(nibName: "FirstTableViewCell", bundle: nil)
self.tableView.register(cellNib, forCellReuseIdentifier: "cell")
let cellNib2 = UINib(nibName: "SecondViewCell", bundle: nil)
self.secondTableView.register(cellNib2, forCellReuseIdentifier: "cell2")
这是我的cellForRowAt函数:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == tableView {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FirstTableViewCell
return cell
}
else {
let cell2 = self.secondTableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! SecondViewCell
return cell2
}
}
答案 0 :(得分:2)
在cellForRowAt
函数中,有一个名为tableView
的局部变量(请看函数头,tableView
是第一个参数的名称),因此检查{{1 }}总是会返回true。这就是为什么您将始终获得第一个单元格的原因。
将该行替换为:
tableView == tableView
通过添加if tableView == self.tableView {}
,您将直接引用类变量self
而不是局部变量。希望这会有所帮助。