[英]NSInternalInconsistencyException: Invalid update in tableView with sections
I have a UITableView with sections from which I'm trying to delete rows. 我有一个UITableView带有要从中删除行的部分。 In switching to AFMotion from BubbleWrap I've started getting a few immutable array errors. 从BubbleWrap切换到AFMotion时,我开始遇到一些不可变的数组错误。 Usually they are fixable by appending a mutableCopy on the object I'm altering before pushing its view controller. 通常,它们可以通过在推送其视图控制器之前在要更改的对象上附加mutableCopy来修复。 For the table with sections, however, I get past the frozen/immutable array error but get an NSInternalInconsistencyException: Invalid update: invalid number of rows in section 0
. 但是,对于带有节的表,我克服了冻结/不可变的数组错误,但得到了NSInternalInconsistencyException: Invalid update: invalid number of rows in section 0
。 I have seen other questions about these and have tried adding beginUpdates
, endUpdates
, etc. but it still won't work. 我看到了有关这些的其他问题,并尝试添加beginUpdates
, endUpdates
等,但仍然无法正常工作。
My data object is a hash that looks like this: 我的数据对象是一个哈希,看起来像这样:
{
"Items" => ["one", "two", "three"],
"More" => ["four", "five", "six"]
}
I pass that data to a new controller when I present it: 提交数据时,我会将其传递给新的控制器:
def tap_items(sender)
controller = ItemsController.alloc.initWithNibName(nil, bundle:nil)
controller.data = @data[:items].mutableCopy
self.presentViewController(
UINavigationController.alloc.initWithRootViewController(controller),
animated:true,
completion: lambda {}
)
end
Then in ItemsController
my table is set up to delete rows with the following: 然后在ItemsController
我的表设置为使用以下命令删除行:
def tableView(tableView, commitEditingStyle:editingStyle, forRowAtIndexPath:indexPath)
if editingStyle == UITableViewCellEditingStyleDelete
rows_for_section(indexPath.section).delete_at indexPath.row
tableView.deleteRowsAtIndexPaths([indexPath],
withRowAnimation:UITableViewRowAnimationFade)
end
end
But that throws the internal inconsistency exception errors. 但这会引发内部不一致异常错误。 I've also tried data[sections[indexPath.section]].delete_at indexPath.row
instead of rows_for_section(indexPath.section).delete_at indexPath.row
. 我也尝试过data[sections[indexPath.section]].delete_at indexPath.row
而不是rows_for_section(indexPath.section).delete_at indexPath.row
。 But that doesn't change anything. 但这并没有任何改变。
Update: Here is the code for my tableView 更新:这是我的tableView的代码
def viewDidLoad
super
@table = UITableView.alloc.initWithFrame(self.view.bounds)
@table.autoresizingMask = UIViewAutoresizingFlexibleHeight
self.view.addSubview(@table)
@table.dataSource = self
@table.delegate = self
end
def sections
data.keys.sort
end
def rows_for_section(section_index)
data[self.sections[section_index]].mutableCopy
end
def row_for_index_path(index_path)
rows_for_section(index_path.section)[index_path.row]
end
def tableView(tableView, titleForHeaderInSection:section)
sections[section]
end
def numberOfSectionsInTableView(tableView)
self.sections.count
end
def tableView(tableView, numberOfRowsInSection: section)
rows_for_section(section).count
end
It sounds like somehow you are not referencing the correct instance of your dataSource. 听起来好像您没有在引用正确的dataSource实例。 You don't show where you are assigning it, ie tableView.dataSource = data
, nor the code for rows_for_section
. 您没有显示要在哪里分配它,即tableView.dataSource = data
,也没有显示rows_for_section
的代码。 I'm wondering whether it has to do with the .mutableCopy
. 我想知道它是否与.mutableCopy
。 Is that necessary for some reason? 由于某种原因,这是否必要?
In my app, I pull in the dataSource directly from the tableView, ie tableView.dataSource
. 在我的应用程序中,我直接从tableView中获取dataSource,即tableView.dataSource
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.