我有两个带有两个xib单元格的tableviews。如何在第二个表中注册第二个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
}
}
在你的cellForRowAt
函数中,有一个名为tableView
的局部变量(查看函数头,tableView
是第一个参数的名称),因此检查tableView == tableView
总是返回true。这就是为什么你总是得到第一个单元格。
将该行替换为:
if tableView == self.tableView {}
通过添加self
,您直接引用类变量tableView
而不是局部变量。希望这可以帮助。