我有一个按字母顺序划分的tableview。我有一个NSMutableArray *drinks
这是我的原始数据源,它从plist中填充NSMutableDictionary's
。在我的viewDidLoad
方法中。然后我遍历我的数组,并在viewWillAppear
方法
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.sections = [[NSMutableDictionary alloc] init];
BOOL found;
// Loop through the whiskys and create our keys
for (NSMutableDictionary *whisky in self.drinks)
{
NSString *c = [[whisky objectForKey:NAME_KEY] substringToIndex:1];
found = NO;
for (NSString *str in [self.sections allKeys])
{
if ([str isEqualToString:c])
{
found = YES;
}
}
if (!found)
{
[self.sections setValue:[[NSMutableArray alloc] init] forKey:c];
}
}
// Loop again and sort the whiskys into their respective keys
for (NSDictionary *whisky in self.drinks)
{
[[self.sections objectForKey:[[whisky objectForKey:NAME_KEY] substringToIndex:1]] addObject:whisky];
}
// Sort each section array
for (NSString *key in [self.sections allKeys])
{
[[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:NAME_KEY ascending:YES]]];
}
[self.tableView reloadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [searchPaths lastObject];
NSString *writeableDBPath = [documentsDirectory stringByAppendingPathComponent:@"ScotchList.plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc]initWithContentsOfFile:writeableDBPath];
self.drinks = tmpArray;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
//Register for application exiting information so we can save data
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
}
所有这一切都很好,花花公子。然后,当我删除一行时,它也可以正常工作。然而,当我进入新视图并返回到tableview时,已删除的行重新出现。所以我需要更新我的原始数据源(饮料),因为每次viewAppears它再次从原始数据源拉取。所以我试着把这个项目(威士忌)放在那一行,然后将它从数组中删除。但它引发了一个sigabrt。这是我的删除行方法。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//DELETE THE ROW FROM THE DATA SOURCE
[self.tableView beginUpdates];
[[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] removeObjectAtIndex:indexPath.row];
NSDictionary *whisky = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
[self.drinks removeObject:whisky];
if ([[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] count] > 0)
{
// Section is not yet empty, so delete only the current row.
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else
{
// Section is now completely empty, so delete the entire section.
[self.sections removeObjectForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView endUpdates];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
这是日志显示的内容:
2012-07-06 15:36:42.454 WhiskyBook[3275:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(0x3998a17b 0x3494395b 0x398d83bd 0x3e6b5 0x32a0cd11 0x328f0d09 0x328f0cbb 0x328f0c95 0x328f09eb 0x328f1369 0x328ef791 0x328dd72d 0x328dd009 0x339a7603 0x339a7233 0x3995a873 0x3995a817 0x39959611 0x398d6be5 0x398d6a71 0x339a634b 0x329037f5 0x3ac09 0x33cc7b20)
libc++abi.dylib: terminate called throwing an exception
(lldb)
我做错了什么?
答案 0 :(得分:0)
我找到了解决问题的方法。我没有尝试从commitEditingStyle方法内部的原始数据源中删除对象,而是为我的master-viewController创建了一个NSDictionary属性,并在我的viewDidLoad中将其插入,然后为其分配了我想要删除的项目。 commitEditingStyle方法。然后在我的viewWillDisappear方法中,我将其从原始数据源中删除。