UITableView分段行问题

时间:2010-08-29 03:58:37

标签: objective-c uitableview

我有一个分段的UITableView使用字典调整教程中的代码(我目前没有链接,但如果你真的需要它我可以挖掘它)。

无论如何,当我收到此错误时,我正试图从列表中删除一个项目(警告:它很长)

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).

我有点获得有关行数的信息,但我很确定如何解决这个问题。

如果您想查看调试器信息,我会粘贴它,因为它太长了:http://pastebin.com/0difSEnM

这是我的代码:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)index {
 NSDictionary *dictionary = [data objectAtIndex:index.section];
 NSMutableArray *array = [NSMutableArray arrayWithArray:[dictionary objectForKey:@"My Wishlists"]];
 NSString *selectedWishlist = [array objectAtIndex:index.row];
 //now remove the item
[array removeObjectAtIndex:index.row];
 [data removeObjectAtIndex:index.section];
 NSDictionary *newdictionary = [NSDictionary dictionaryWithObject:[NSArray arrayWithArray:array] forKey:@"My Wishlists"];
 [data insertObject:newdictionary atIndex:0];
// [data addObject:newdictionary];
 [mainTableView reloadData]; // tried without and with this, no difference
 [mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationTop];
}

谢谢! 克里斯蒂安斯图尔特

1 个答案:

答案 0 :(得分:1)

看起来这是你的问题:

[data insertObject:newdictionary atIndex:0];

来自NSMutableArray的insertObject:atIndex:

的文档
  

讨论如果 index 已经存在   占用,索引和的对象   通过加1来改变   他们的指数可以腾出空间。

请尝试使用replaceObjectAtIndex:withObject: