NSRangeException - 超出边界的索引1 [0 .. 0]

时间:2016-03-28 12:14:24

标签: ios swift uitableview

我有一个带有静态单元格的tableViewController。我要做的是在其中一个静态单元格中添加一个带有动态单元格的tableView。

self.notTakenDates是一个数组,self.takenDates

也是如此

所以我有这段代码:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    var numberOfSections: Int?
    var table: String = ""  //Just so I know for what table I run the code
    if tableView == self.progressTableView {
        table = "progressTableView"
        numberOfSections = 2
    }
    if tableView == self.tableView {
        table = "self.tableView"
        numberOfSections = 1
    }
    print("Number of sections: \(numberOfSections) for \(table)")
    return numberOfSections ?? 0
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var numberOfRows: Int = 0
    if tableView == self.progressTableView {
        if section == 0 { numberOfRows = self.notTakenDates.count }
        if section == 1 { numberOfRows = self.takenDates.count }
        print("progressTableview - Number of rows: \(numberOfRows) in section \(section)")
    }
    if tableView == self.tableView {
        numberOfRows = 8
        print("self.tableview - Number of rows: \(numberOfRows) in section \(section)")
    }

    return numberOfRows
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = UITableViewCell()
    var table: String = ""
    if tableView == self.tableView {
        table = "self.tableView"
        //Switch statement is for now - I think there must be a way to get those static cells using some shortcut?
        switch indexPath.row {
        case 0:
            cell = self.nameCell
        case 1:
            cell = self.dosageCell
        case 2:
            cell = self.noteCell
        case 3:
            cell = self.periodCell
        case 4:
            cell = self.remindersCell
        case 5:
            cell = self.progressCell
        case 6:
            cell = self.blankCell
        case 7:
            cell = self.buttonCell
        default:
            cell = UITableViewCell()
        }
    } else if tableView == self.progressTableView {
        table = "self.progressTableView"
        let progressCell = tableView.dequeueReusableCellWithIdentifier("ProgressCell") as! ProgressTableViewCell
        progressCell.dateLabel.text = "Hello" //Just for now - will be changed after I figure out how to fix this error
        cell = progressCell
    }
    return cell
}

我在输出中看到的是:

Number of sections: Optional(1) for self.tableView
self.tableview - Number of rows: 8 in section 0
Number of sections: Optional(2) for progressTableView
2016-03-28 14:08:28.257 MyApp[1166:75146] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

我做错了什么?它必须是numberOfRowsInSection中的内容,但我无法弄清楚。

0 个答案:

没有答案