UISegmentController与UITableviewC

UISegmentController与UITableviewC

我有一个具有段控制器的表视图,它分别具有两个段类别1和2。当我将商品添加到类别1时,它做的很好,但是当我将商品添加到类别2时,应用程序崩溃,说“由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“试图将第0行插入第0节,但更新后的第0部分中只有0行。
这是我插入表视图的代码。

对于类别1:

func itemDetailViewController(controller: ItemDetailViewController, didFinishAddingItem item: NoToDoItem) {
    let newRowIndex = items.count

    items.append(item)

    let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0)
    let indexPaths = [indexPath]
    tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)

    dismissViewControllerAnimated(true, completion: nil)
}


对于类别2:

func itemDetailViewController(controller: ItemDetailViewController, didFinishAddingNotSureItem notSureItem: NotSureItem) {
    let newRowIndex = notSureItems.count

    notSureItems.append(notSureItem)

    let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0)
    let indexPaths = [indexPath]
    tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)

    dismissViewControllerAnimated(true, completion: nil)
}

最佳答案

您确定表格显示的是当前添加到的类别吗?尝试这个:

func itemDetailViewController(controller: ItemDetailViewController, didFinishAddingItem item: NoToDoItem) {
    let newRowIndex = items.count

    items.append(item)
    if segmentBar.selectedSegmentIndex == 0 {
        let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0)
        let indexPaths = [indexPath]
        tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
    }
    dismissViewControllerAnimated(true, completion: nil)
}


并对didFinishAddingNotSureItem执行相同操作。

还要确保在更改类别时重新加载表视图。

关于ios - UISegmentController与UITableviewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33229943/

10-10 21:07