我有一个表视图,它有一个段控制器,它有两个段分别为1和2。当我将一个项目添加到类别1时,它完美地完成了但是当我将项目添加到类别2时,它会崩溃应用程序,说明“由于未捕获的异常而终止应用程序' NSInternalInconsistencyException'”,原因:&# 39;尝试将第0行插入第0部分,但在更新后第0部分只有0行#39; 这是我的插入表视图的代码。
对于第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)
}
答案 0 :(得分:1)
您确定您的表格显示的是您目前添加的相同类别吗?试试这个:
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
执行相同操作。
另外,请确保在更改类别时重新加载tableview。
答案 1 :(得分:0)
UITableView必须始终与数据源保持同步。如果数据源可以在后台线程中更改,则必须特别小心。
当某些内容添加到数据源时,请尽快调用beginUpdate / insert / endUpdate。所以试试这个:
SELECT b.BILLCOD AS BILLCOD, t.TOTAL - b.TOTAL AS TOTAL
FROM Billings b INNER JOIN
(
SELECT bh1.HISTORYCOD, bh1.BILLCOD, bh1.TOTAL
FROM Billings_History bh1 INNER JOIN
(
SELECT BILLCOD, MAX(HISTORYCOD) AS MAXHISTORYCOD
FROM Billings_History
GROUP BY BILLCOD
) bh2
ON bh1.HISTORYCOD = bh2.MAXHISTORYCOD AND bh1.BILLCOD = bh2.BILLCOD
) t
ON b.BILLCOD = t.BILLCOD
PS:正确设置了对数据源模型对象tableView.beginUpdates()
let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0)
let indexPaths = [indexPath]
tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
tableView.endUpdates()
的信任,并且notSureItems
函数返回正确的计数。
编辑:发布OP评论
您正在尝试在错误的索引处添加行。表视图索引从0开始,因此您应该推导出新的单元格索引:
numberOfRowsInSection
另外,在notSureItems.append(notSureItem)
let newRowIndex = notSureItems.count - 1
和itemDetailViewController
函数中设置一个断点来检查模型值。