致命错误:索引超出范围

时间:2017-08-28 15:01:39

标签: swift uitableview

我试图在表格视图中添加部分。但我得到一个致命的错误:指数超出范围。我的代码有什么问题?怎么解决?有没有更好的方式以编程方式添加部分?

这是我的日期单元格引用:https://github.com/KoheiHayakawa/DateCell但引用没有任何部分。这是我的代码:

var dateArray : [[String: AnyObject]] = []
var dateArray2 : [[String: AnyObject]] = []

viewDidLoad中

    let itemOne = [titleKey: "Start Time", dateKey: nextFirst] as [String : Any]
    let itemTwo = [titleKey: "End Time", dateKey: nextFirst] as [String : Any]
    let itemThree = [titleKey: "Apply to All"]
    self.dateArray = [itemOne as Dictionary<String, AnyObject>, itemTwo as Dictionary<String, AnyObject>, itemThree as Dictionary<String, AnyObject>]

    let dayOne = [titleKey: "31/8"]
    let dayTwo = [titleKey: "1/9"]
    let dayThree = [titleKey: "2/9"]
    let dayFour = [titleKey: "3/9"]
    let dayFive = [titleKey: "4/9"]
    let daySive = [titleKey: "5/9"]
    let daySeven = [titleKey: "6/9"]

    self.dateArray2 = [dayOne as Dictionary<String, AnyObject>, dayTwo as Dictionary<String, AnyObject>, dayThree as Dictionary<String, AnyObject>,dayFour as Dictionary<String, AnyObject>, dayFive as Dictionary<String, AnyObject>, daySive as Dictionary<String, AnyObject>, daySeven as Dictionary<String, AnyObject>]

func localeChanged(_ notif:NSNotification){
    myTableView.reloadData()
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 0 {
        return "Next week\nDate...."
    } else {
        return "Specific date and time"
    }
}

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

    if (self.hasInlineDatePicker()) {
        return self.dateArray.count+1;
    }

    if section == 0{
        return self.dateArray.count
    }else{
        return self.dateArray2.count
    }

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell: UITableViewCell?
    var cellID = kOtherCell

    if (self.indexPathHasPicker(indexPath)) {
        cellID = kDatePickerCell;
    } else if (self.indexPathHasDate(indexPath)) {
        cellID = kDateCell;
    }

    cell = tableView.dequeueReusableCell(withIdentifier: cellID)

    var modelRow = indexPath.row
    if (self.datePickerIndexPath != nil && self.datePickerIndexPath!.row <= indexPath.row) {
        modelRow -= 1
    }

    let itemData = self.dateArray[modelRow]
    let dateData = self.dateArray2[indexPath.row]

    if (indexPath.section == 0) {
        // proceed to configure our cell
        if (cellID == kDateCell) {
            cell!.textLabel!.text = itemData[titleKey] as? String
            cell!.detailTextLabel!.text = self.dateFormatter.string(from: itemData[dateKey] as! Date)
        } else if (cellID == kOtherCell) {
            cell!.textLabel!.text = itemData[titleKey] as? String
        }
    } else if (indexPath.section == 1) {
        if (cellID == kOtherCell) {
           cell!.textLabel!.text = dateData[titleKey] as? String
        }
    }
    return cell!
}

App Layout:CellForRowAt第一部分有一个datePicker Cell隐藏 Layout

0 个答案:

没有答案