尝试在UITableView中插入新行时,iOS应用程序崩溃

wst*_*str 0 iphone objective-c uitableview ios

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:YES];
[self.tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)

这段代码崩溃我的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];
}
Run Code Online (Sandbox Code Playgroud)

并且错误的图片:

错误

Emp*_*ack 5

numberOfRowsInSection每当您对表视图的数据源进行任何更改时,该方法都应返回正确的行数.

例如,如果表中的总行数为5,并且如果要再添加一行,则该numberOfRowsInSection方法应返回6.

它可以通过将数据放入array或中轻松实现dictionary.如果要添加或删除行,只需更新数组或字典即可.只是返回[array count]或[dictionary count]从numberOfRowsInSection方法.