因此,我尝试创建一个基本的待办事项列表应用程序,但是我一直收到此错误,并且我不知道它的真正含义,也不知道如何解决它,任何帮助将不胜感激...这是我的代码用于 View Controller
类RemfoFightStyle:UIViewController,UITextFieldDelegate,UITableViewDataSource {
@IBOutlet弱var tableView:UITableView!
@IBOutlet弱var addTextField:UITextField!
var fightStyle: [String] = ["Light to heavy punches",
"Lots of mix ups"]
@IBAction func addButton(_ sender: UIButton) {
insertNewStyleTitle()
}
func insertNewStyleTitle() {
fightStyle.append(addTextField.text!)
let indexPath = IndexPath(row: fightStyle.count - 2, section: 1)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
addTextField.text = ""
view.endEditing(true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fightStyle.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let fightTitle = fightStyle[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "NewCell")! as! NewCell
_ = fightTitle.count
cell.bigLabel
.text = fightStyle[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
fightStyle.remove(at: indexPath.row)
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
}
}
}
最佳答案
let indexPath = IndexPath(row: fightStyle.count - 2, section: 1)
行和节是从零开始的。您只有一个部分,因此第二个参数应该是0
。
关于swift - 线程1 : Exception: “attempt to insert row 1 into section 1, but there are only 1 sections after the update” ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63912134/