Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在两个静态UITableViewCell之间插入带有UIPickerView的行时发生NSRangeException_Ios_Swift_Uitableview_Uipickerview - Fatal编程技术网

Ios 在两个静态UITableViewCell之间插入带有UIPickerView的行时发生NSRangeException

Ios 在两个静态UITableViewCell之间插入带有UIPickerView的行时发生NSRangeException,ios,swift,uitableview,uipickerview,Ios,Swift,Uitableview,Uipickerview,我有一个有四个单元格的静态UItableView。当我尝试使用insertRowsAtIndexPath tableView方法在第三个单元格后插入一行,并将UIPickerView添加到添加的行时,我遇到了以下错误:未捕获异常“NSRangeException”,原因:'-[\uu NSArrayI objectAtIndex:::]:索引4超出边界[0..3] 我试图实现的是iOS日历应用程序中的在线日期选择器,但使用UIPickerView。我想在选中/取消选中的第三行的正下方显示一个UI

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

我试图实现的是iOS日历应用程序中的在线日期选择器,但使用UIPickerView。我想在选中/取消选中的第三行的正下方显示一个UIPicker/隐藏一个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)
}

您是否添加了异常断点以准确捕获问题发生的位置?如果您能够提供可以运行的完整源代码,那么调查问题就更容易了。