Xcode exc_bad_访问代码=在TableView中删除单元格时exc_i386_gpflt

Xcode exc_bad_访问代码=在TableView中删除单元格时exc_i386_gpflt,xcode,swift,Xcode,Swift,在xcode 6中,删除待办事项列表中的单元格时,我随机得到一个错误 exc_坏_访问代码=exc_i386_gpflt 我有两个视图-在数组toDoList中输入项的第二个视图 在“第一个”中,我显示toDoList中的所有项目,并在以下情况下选择删除第一页的代码: import UIKit var toDoList: [String] = [] class FirstViewController: UIViewController, UITableViewDelegate, UITab

在xcode 6中,删除待办事项列表中的单元格时,我随机得到一个错误 exc_坏_访问代码=exc_i386_gpflt

我有两个视图-在数组toDoList中输入项的第二个视图 在“第一个”中,我显示toDoList中的所有项目,并在以下情况下选择删除第一页的代码:

import UIKit


var toDoList: [String] = []

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
//var cell = UITableViewCell()

@IBOutlet weak var itemList: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

   return toDoList.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

   var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

  cell.textLabel.text = toDoList[indexPath.row]

    return cell
}

override func viewWillAppear(animated: Bool) {


    if var storedItems: AnyObject = NSUserDefaults.standardUserDefaults().objectForKey("items"){

        toDoList = []

        for var i = 0; i < storedItems.count; ++i{

        toDoList.append(storedItems[i] as NSString)
        }
    }

    itemList.reloadData()

}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){

    if editingStyle == UITableViewCellEditingStyle.Delete{


    toDoList.removeAtIndex(indexPath.row)

        let saveData = toDoList

        NSUserDefaults.standardUserDefaults().setObject(saveData, forKey: "items")

        NSUserDefaults.standardUserDefaults().synchronize()

        itemList.reloadData()
    }

}

}导致错误的原因是

itemList.reloadData()
在你的职责范围内

因此,要解决您的问题,请删除该行,然后将该行添加到toDoList.RemoveAtIndexIndexXPath.row之后:

此方法的作用是:删除由索引路径数组指定的行,并提供一个选项来设置删除的动画。来自Xcode快速帮助

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)