【问题标题】:Swift fatal error: array index out of rangeSwift 致命错误:数组索引超出范围
【发布时间】:2014-12-24 20:10:31
【问题描述】:

我正在制作一个待办事项列表应用程序,但是当我尝试从列表中删除某些内容时,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()


        }
    }
}

正在eventList.removeAtIndex(indexPath.row) 处创建一个断点,即 EXC_BAD_INSTRUCTION。

【问题讨论】:

    标签: ios arrays swift


    【解决方案1】:

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

    if editingStyle == .Delete {
    
        eventList.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
    
       // ...
    }
    

    否则表格视图会调用原始的数据源方法 行数,导致超出范围错误。

    也可以在修改数据源的时候调用tableView.reloadData(),但是上面的方法 提供更好的动画。

    【讨论】:

      【解决方案2】:

      这意味着您正在尝试访问超出eventList 范围的索引indexPath.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()
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多