Мой первый вопрос по stackoverflow (<< n00b).Я работаю над своим первым проектом, включающим UITableViews и списки, и у меня возникла проблема. </p>
Список свойств состоит из словаря, содержащего 3 раздела / категории (каждый массив) и несколько записей вкаждый из них.
Списки загружаются просто отлично.Проблема не возникнет, пока я не попытаюсь сделать возможным удаление отдельных записей из списка.
Вот мой код:
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//make array of the objects and remove the selected object from that array.
NSMutableArray *delArray = [faves objectForKey:[entries objectAtIndex:indexPath.section]];
[delArray removeObjectAtIndex:indexPath.row];
//set entries to the value of the array, now sans the removed object.
self.entries = delArray;
[delArray release];
//write the updated entries to the plist file.
NSArray *rootPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [rootPath objectAtIndex:0];
NSString *plistFile = [docsPath stringByAppendingPathComponent:@"Data.plist"];
[faves writeToFile:plistFile atomically:YES];
//update the actual tableview. This is where things go awry.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; //ends up trying to load the sections instead of the entries = wrong number of list items returned = crash!
//[tableView reloadData]; //works if I only use this line, but the list updates wrong, showing each entry as a section header.
}
}
Запись удалена из списка корректно, нотаблица не обновляется правильно и вызывает сбой приложения.Я включил код ошибки ниже.
2011-09-23 18:40:19.732 MyApp[10314:b303] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:974
2011-09-23 18:40:19.734 MyApp[10314:b303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (5) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'
Первое число (5) соответствует количеству записей, которые должны остаться в соответствующей категории, но второе число (3) относится кколичество категорий.Как я уже говорил выше, запись удаляется из списка, а не из таблицы.