在两个静态UITableViewCells之间插入带有UIPickerView的行时出现NSRangeException



我有一个静态UItableView与四个单元格。当我尝试使用insertRowsAtIndexPath tableView方法在第三个单元格后插入一行并将UIPickerView添加到添加的行时,我得到了这个错误:未捕获的异常'NSRangeException',原因:'-[__NSArrayI objectAtIndex:]:索引4超出界限[0 ..]3)

我想要实现的是在iOS日历应用程序的内联日期picker,但使用一个UIPickerView。我想在被选中/取消选中的第三行下面显示一个picker/隐藏一个pickerView。我还想对最后一行做同样的处理。

注意:添加行与pickerView工作时,我添加以下的第四个单元格,但不工作时,插入其他单元格之间的单元格

如果有另一种方法可以实现这一点,而不需要首先在故事板中创建UIPickerView,我会很高兴。

var pickerIsVisible = false
func hidePicker() {
    pickerIsVisible = false
    let indexPathPicker = NSIndexPath(forRow: 3, inSection: 0)
    tableView.beginUpdates()
    tableView.deleteRowsAtIndexPaths([indexPathPicker], withRowAnimation: .Fade)
    tableView.endUpdates()
}
func showPicker() {
    pickerIsVisible = true
    let indexPathPicker = NSIndexPath(forRow: 3, inSection: 0)
    tableView.beginUpdates()
    tableView.insertRowsAtIndexPaths([indexPathPicker], withRowAnimation: .Fade)
    tableView.endUpdates()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.section == 0 && (indexPath.row == 3 && pickerIsVisible) {
        var cell = tableView.dequeueReusableCellWithIdentifier("PickerCell") as UITableViewCell!
        if cell == nil {
            cell = UITableViewCell(style: .Default, reuseIdentifier: "PickerCell")
            cell.selectionStyle = .None
            let picker = UIPickerView(frame: cell.frame)
            picker.tag = 100
            picker.delegate = self
            picker.dataSource = self
            cell.contentView.addSubview(picker)
        }
        return cell
    } else {
        return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
    }
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 && pickerIsVisible {
        return 5
    } else {
        return super.tableView(tableView, numberOfRowsInSection: section)
    }
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.section == 0 && indexPath.row == 2 {
        tableView.deselectRowAtIndexPath(indexPath, animated: false)
        noteTextField.resignFirstResponder()
        pickerIsVisible ? hidePicker() : showPicker()
    }
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.section == 0 && pickerIsVisible && indexPath.row == 3 {
        return 117
    } else {
        return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
    }
}
override func tableView(tableView: UITableView, var indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int {
    if indexPath.section == 0 && indexPath.row == 3 && pickerIsVisible {
        indexPath = NSIndexPath(forRow: 0, inSection: indexPath.section)
    }
    return super.tableView(tableView, indentationLevelForRowAtIndexPath: indexPath)
}

通过修改这些方法解决:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.section == 0 && pickerIsVisible && indexPath.row == 3 {
        return 117
    }
    if indePath.row == 4 && pickerIsVisisble {
        return 44
    }
    return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}
override func tableView(tableView: UITableView, var indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int {
    if indexPath.row == 3 || indexPath.row == 4 && pickerIsVisible {
        indexPath = NSIndexPath(forRow: 0, inSection: indexPath.section)
    }
    return super.tableView(tableView, indentationLevelForRowAtIndexPath: indexPath)
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.section == 0 && (indexPath.row == 3 && pickerIsVisible) {
        let cell = UITableViewCell(style: .Default, reuseIdentifier: nil)
        cell.selectionStyle = .None
        let frame = CGRect(x: 0, y: 0, width: cell.frame.size.width, height: 200)
        let picker = UIPickerView(frame: frame)
        picker.tag = 100
        picker.delegate = self
        picker.dataSource = self
        cell.contentView.addSubview(picker)
        return cell
    }
    if pickerIsVisible && indexPath.row == 4 {
            let cell = tableView.dequeueReusableCellWithIdentifier("Time Cell") as UITableViewCell!
            return cell
    }
    return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
}

最新更新