[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:YES];
[self.tableView endUpdates];
这段代码崩溃我的iOS应用程序并返回以下错误:
无效更新:第0节中的行数无效。更新(0)后现有部分中包含的行数必须等于更新前该部分中包含的行数(0) ,加上或减去从该部分插入或删除的行数(插入1个,删除0个)。
有人可以告诉我为什么吗?那怎么说更新后的行数为零,同时说 1插入,0删除?
更新
当我在运行时收到推送通知时,会触发上面的代码。当发生这种情况时,委托将接收到的数据添加到存储在.plist文件中的字典(表视图用于填充其单元格),然后调用RootViewController中的表视图所在的自定义方法。此方法执行上面的代码,然后崩溃。但是,当我在[theDictionary count]
中记录- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
时,数字会在崩溃之前以正确的方式递增。
另外值得注意的是,当我记录self.tableview
时,我会在RootViewController在启动时加载时以及在接收推送通知时收到不同的“内存地址”。也许这与它有关?
这是我的- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [theDictionary count];
}
错误的图片:
答案 0 :(得分:5)
每当您对表视图的数据源进行任何更改时,numberOfRowsInSection
方法都应返回正确的行数。
例如,如果表中的总行数为5,并且您要再添加一行,则numberOfRowsInSection
方法应返回6.
通过将数据放在array
或dictionary
中轻松实现。如果要添加或删除行,只需更新数组或字典即可。然后从[array count]
方法返回[dictionary count]
或numberOfRowsInSection
。