我有一个 TableView ,它有一个段 Controller ,它分别有两个段类别 1 和 2。当我将一个项目添加到类别 1 时,它完美地完成了,但是当我将一个项目添加到类别 2 时,它使应用程序崩溃,提示“由于未捕获的异常‘NSInternalInconsistencyException’而终止应用程序,原因:‘尝试将第 0 行插入第 0 节,但更新后第 0 节中只有 0 行”。 这是我插入 TableView 的代码。
对于类别 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
做同样的事情。
还要确保在更改类别时重新加载表格 View 。
关于ios - UISegmentController 与 UITableviewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33229943/