Ios Swift致命错误:数组索引超出范围

Ios Swift致命错误:数组索引超出范围,ios,arrays,swift,Ios,Arrays,Swift,我正在制作一个待办事项列表应用程序,但当我试图从列表中删除某些内容时,xcode给了我一个错误,上面写着“致命错误:数组索引超出范围”。有人能告诉我是什么原因导致了这种情况的发生 import UIKit class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { override func viewDidLoad() { super.viewDi

我正在制作一个待办事项列表应用程序,但当我试图从列表中删除某些内容时,xcode给了我一个错误,上面写着“致命错误:数组索引超出范围”。有人能告诉我是什么原因导致了这种情况的发生

import UIKit

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    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 eventList.count

    }


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

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

        cell.textLabel?.text = eventList[indexPath.row]

        return cell
    }

    override func viewWillAppear(animated: Bool) {

        if var storedEventList : AnyObject = NSUserDefaults.standardUserDefaults().objectForKey("EventList") {

            eventList = []

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

                eventList.append(storedEventList[i] as NSString)
            }

        }
    }

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

        if(editingStyle == UITableViewCellEditingStyle.Delete) {

            eventList.removeAtIndex(indexPath.row)

            NSUserDefaults.standardUserDefaults().setObject(eventList, forKey: "EventList")
            NSUserDefaults.standardUserDefaults().synchronize()


        }
    }
}
导入UIKit
类SecondViewController:UIViewController、UITableViewDelegate、UITableViewDataSource{
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回eventList.count
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
var cell=UITableViewCell(样式:UITableViewCellStyle.Default,reuseIdentifier:“单元格”)
cell.textlab?.text=eventList[indexPath.row]
返回单元
}
覆盖功能视图将出现(动画:Bool){
如果var storedEventList:AnyObject=NSUserDefaults.standardUserDefaults().objectForKey(“事件列表”){
事件列表=[]
对于变量i=0;i

事件列表.removateIndex(indexath.row)
上也创建了一个断点,表示EXC\u BAD\u指令。

从数据源数组中删除该项是不够的。 您还必须告诉表视图该行已被删除:

if editingStyle == .Delete {

    eventList.removeAtIndex(indexPath.row)
    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)

   // ...
}
否则,表视图将调用原始表的数据源方法 行数,导致超出范围错误

或者,您可以在修改数据源时调用
tableView.reloadData()
,但不能使用上述方法
提供了更好的动画。

这意味着您正试图访问超出
事件列表范围的索引
indexath.row
。要解决此问题,请尝试:

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

    if(editingStyle == .Delete && indexPath.row < eventList.count) {
        eventList.removeAtIndex(indexPath.row)
        tableView.reloadData()

        NSUserDefaults.standardUserDefaults().setObject(eventList, forKey: "EventList")
        NSUserDefaults.standardUserDefaults().synchronize()
    }
}
func tableView(tableView:UITableView,committeeditingstyle编辑样式:UITableView单元格编辑样式,forRowAtIndexPath-indexath:nsindexath){
if(editingStyle=.Delete&&indexPath.row