2011-08-28 41 views
0
插入新行
[self.tableView beginUpdates]; 
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:YES]; 
[self.tableView endUpdates]; 

这段代码崩溃我的iPhone应用程序,并返回以下错误时:的iOS应用程序崩溃试图UITableView的

无效更新:在第0的数行数无效更新(0)后现有节中包含的行必须等于更新前(0)该节中包含的行数加上或减去从该节插入或删除的行数(插入1个,删除0个)。

有人能告诉我为什么吗?又如何说它更新后的行数为零,当它同时表示1插入,0删除

更新

的代码时,我在运行时收到一个推送通知上述被触发。发生这种情况时,委托会将接收到的数据添加到存储在.plist文件中的字典中(表视图用来填充其单元格),然后调用表视图所在的RootViewController中的自定义方法。此方法执行上面的代码,然后执行崩溃。但是,当我登录- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section时,[theDictionary count]的数字会增加,正如右边的在崩溃之前。

另外值得注意的是,当我登录self.tableview我收到一个不同的“内存地址”从RootViewController启动时加载时,并收到推送通知。也许这跟它有关系?

这是我- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [theDictionary count]; 
} 

和错误的图片:

error

+0

那段代码在哪里? – BoltClock

+0

您可以向我们展示numberofrowsforsection方法和numberofsection方法的代码 – Robin

+0

我已更新该帖子。 – wstr

回答

5

numberOfRowsInSection方法应该返回正确的行数,只要你做出任何改变表视图的数据源。

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

其通过在array具有数据容易地实现或dictionary。如果要添加或删除行,只需更新数组或字典。并从numberOfRowsInSection方法返回[array count][dictionary count]

+0

我已更新该帖子的更多信息。 – wstr