这些都是我演示中的所有代码:
import UIKit
class TableViewController: UITableViewController {
var numberOfCell = 0
@IBAction func addAction() {
numberOfCell = numberOfCell + 1
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfCell + 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "FirstCell", for: indexPath)
let addButton = cell.viewWithTag(99) as! UIButton
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath)
let deleteButton = cell.viewWithTag(100) as! UIButton
let textField = cell.viewWithTag(101) as! UITextField
return cell
}
} else {
return super.tableView(tableView, cellForRowAt: indexPath)
}
}
}
我在故事板文件的属性检查器中设置了单元格的标识符,没有注册单元格,一切都是这样:
Tap Add button, show a new cell with a button and a textfield.
但是在我的项目中,我使用相同的方式,当我点按“添加”按钮时,应用程序将崩溃,并报告此错误:
***因未捕获的异常而终止应用 'NSInternalInconsistencyException',原因:'无法将单元格出列 带标识符SecondCell - 必须注册一个nib或类 标识符或连接故事板'
中的原型单元格
我已尝试使用此代码注册单元格:
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "SecondCell")
但如果我这样做,应用会在let deleteButton = cell.viewWithTag(100) as! UIButton
和let textField = cell.viewWithTag(101) as! UITextField
时崩溃,因为它们是零。
我认为我的项目和演示没有差异,我不知道如何处理这个问题。
答案 0 :(得分:0)
如果您使用的是nib文件,则需要在tableview中注册您的单元格。
self.tableView.register(UINib(nibName: "nibName", bundle: nil), forCellReuseIdentifier: "CellIdentifier")