swift - 线程1 : Exception: “attempt to insert row 1 into section 1, but there are only 1 sections after the update”

标签 swift xcode uitableview error-handling xcode11

因此,我尝试创建一个基本的待办事项列表应用程序,但是我一直收到此错误,并且我不知道它的真正含义,也不知道如何解决它,任何帮助将不胜感激...这是我的代码用于 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/

相关文章:

ios - 在 UIView 上模拟 "falling"对象

swift - 柯里化(Currying)所有 swift 函数参数,但不调用该函数

json - 如何通过 Swift 发送 POST 请求?

ios - 2个imageView ios的碰撞

ios - 如何计算内部具有堆栈 View 的自定义单元格的大小?

ios - `onNotificationOpened` 当应用程序被杀死或从 iOS 内存中删除时不会被调用

ios - 下载数据后 TableView 未更新

c++ - CMake 生成的 Xcode 项目不显示头文件

iphone - 在单击取消按钮之前,搜索栏不会显示结果

ios - 在 swift 中访问委托(delegate)函数中选定的单元格部分