使用新数据重新加载tableview会导致数组索引超出范围错误

时间:2015-04-06 21:03:21

标签: ios cocoa-touch swift

我一直在自言自语,遇到了一个我无法弄清楚的问题。我有一个视图控制器,它提供事件(自定义类对象数组)列出的项目的表视图,并按当前日期过滤。该程序按预期工作,直到我尝试更改天数(由PrevButton和NextButton控制)。加载另一个表视图时,程序崩溃,数组索引超出范围错误。任何人都可以向我解释为什么会这样吗?我该如何解决这个问题?视图控制器的代码如下:

import UIKit

var finishedEvent = Event()

class EventSelectionView: UIViewController {


    @IBOutlet weak var EventLabel: UILabel!
    @IBOutlet weak var tableView: UITableView!

    var items:[String] = []


    override func viewDidLoad() {
        super.viewDidLoad()

        EventLabel.text = selectedDate

        items.removeAll()

        println(items.isEmpty)


        for var index = 0; index < EventArray.count; ++index {
            println("in the loop")
            println(EventArray[index].due)
            println(selectedDate)
            if EventArray[index].due.rangeOfString(selectedDate) != nil{
                items.append(EventArray[index].name)
                println(items[index])
            }

        }
        println("After loop")
        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        // Do any additional setup after loading the view.
    }

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

        var tempEvent = Event()

        cell.textLabel?.text = self.items[indexPath.row]

        for var index = 0; index < EventArray.count; index++ {

            if (EventArray[index].name == self.items[indexPath.row]){

                tempEvent = EventArray[index];

            }
        }
        println(tempEvent.ODWM)
        if (tempEvent.ODWM == "DAILY"){

            cell.backgroundColor = UIColor.greenColor()
        } else if (tempEvent.ODWM == "WEEKLY"){

            cell.backgroundColor = UIColor.cyanColor()
        } else if (tempEvent.ODWM == "MONTHLY"){

            cell.backgroundColor = UIColor.purpleColor()
        } else{

        cell.backgroundColor = UIColor.orangeColor()
        }
        return cell
    }

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {


    }

    @IBAction func FinishEventButton(sender: AnyObject) {

        let indexPath = tableView.indexPathForSelectedRow();

        let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;

        for var index = 0; index < EventArray.count; index++ {
            println(EventArray[index].name);
            println(currentCell.textLabel!.text);
            if (EventArray[index].name == currentCell.textLabel!.text){
                finishedEvent = EventArray[index];

            }
    }

        self.performSegueWithIdentifier("finishEventSegue", sender: nil?)

    }

    @IBAction func PrevButton(sender: AnyObject) {

        var tempDate = myDate()

        for var index = 0; index < calLoop.calendar.count; ++index{

            if calLoop.calendar[index].date == selectedDate{
                tempDate = calLoop.calendar[index - 1]
            }
        }

        selectedDate = tempDate.date

    }

    @IBAction func NextButton(sender: AnyObject) {

        var tempDate = myDate()

        for var index = 0; index < calLoop.calendar.count; ++index{

            if calLoop.calendar[index].date == selectedDate{
                tempDate = calLoop.calendar[index + 1]
            }
        }

        selectedDate = tempDate.date

    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

1 个答案:

答案 0 :(得分:0)

在循环中,如果EventArray[index].due.rangeOfString(selectedDate)返回nil,则不会将项目附加到items数组。此时,您的items数组的成员数少于EventArray

下一次循环时,index1数组中的项目数itemsprintln(items[index]),因此您会在{{1}中获得超出范围的索引}}

println更改为:

println(items.last!)