当我向左滑动以删除单元格时,应用程序崩溃

问题描述 投票:-4回答:1

当我每次向左滑动以删除我的应用程序崩溃时运行我的应用程序,它指向线程1:信号SIGABRT“我相信这是我的代码的一部分导致它。

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
          allItems[indexPath.section].removeAtIndex(indexPath.row)
          tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
          let newData = DataItem(title: "New Data", subtitle: "", imageName: nil)
          allItems[indexPath.section].append(newData)
          tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        }
      }

      override func setEditing(editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)

        if editing {
          tableView.beginUpdates()

          for (index, sectionItems) in allItems.enumerate() {
            let indexPath = NSIndexPath(forRow: sectionItems.count, inSection: index)

            tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
          }

          //tableView.beginUpdates()
          tableView.setEditing(true, animated: true)
          tableView.endUpdates()
        } else {
          tableView.beginUpdates()
}
ios swift
1个回答
0
投票

您可以像这样修改代码:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        allItems[indexPath.section].removeAtIndex(indexPath.row)
        tableView.reloadData()
    } else if editingStyle == .Insert {
        let newData = DataItem(title: "New Data", subtitle: "", imageName: nil)
        allItems[indexPath.section].append(newData)
        tableView.reloadData()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.