Я едва работаю над новым проектом.Основная структура - это tableView в RootViewController, где в некоторых строках отображаются текущие статусы и некоторая информация для пользователя.Сейчас я работаю над режимом редактирования, который не будет работать.
На панели навигации есть кнопка, которая вызывает edit
, вот код:
- (void)edit {
NSLog(@"Edit pressed");
if (edit) {
NSLog(@"Deactivate edit\n");
edit = NO;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Bearbeiten" style:UIBarButtonItemStyleBordered target:self action:@selector(edit)];
[contentTableView setEditing:NO animated:YES];
} else {
NSLog(@"Activate edit\n");
edit = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Fertig" style:UIBarButtonItemStyleDone target:self action:@selector(edit)];
[contentTableView setEditing:YES animated:YES];
}
}
Мой viewController является подклассомиз UITableViewController
и contentTableView
не ноль ... Я реализовал следующие методы:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSMutableDictionary *oldDict = [[NSMutableDictionary alloc] initWithDictionary:[saver read]];
NSDictionary *movingDict = [[oldDict objectForKey:@"content"] objectAtIndex:sourceIndexPath.row];
[[oldDict objectForKey:@"content"] removeObjectAtIndex:sourceIndexPath.row];
[[oldDict objectForKey:@"content"] insertObject:movingDict atIndex:destinationIndexPath.row];
[saver write:oldDict];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableDictionary *oldDict = [[NSMutableDictionary alloc] initWithDictionary:[saver read]];
[[oldDict objectForKey:@"content"] removeObjectAtIndex:indexPath.row];
[saver write:oldDict];
[contentTableView beginUpdates];
[contentTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; //This doesn't work!
[contentTableView endUpdates];
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"Entfernen";
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
Я скопировал это из другого проекта и изменил некоторые части кода, но основные вещи я не сделалкоснитесь!
Что происходит?
Когда я нажимаю Edit («Bearbeiten»), вызывается функция «edit».Она заменяет кнопку «Готово» («Fertig») и записывает правильный журнал:
> 2011-04-27 13:35:44.338 MyApp[1113:207] Edit pressed
> 2011-04-27 13:35:44.339 MyApp[1113:207] Activate edit
Если я перемещаю ячейку, появляется кнопка «Удалить» («Entfernen»), а когда я нажимаю ее, кнопкаисчезает, но строка все еще там, но она удаляется из содержимого.
Кто-нибудь знает, где я делаю что-то неправильно или где я что-то забыл?
Заранее спасибо, mavrick3.
Редактировать 1:
Я забыл сказать: метод [tableView reloadData];
не вызывает numberOfRowsInSection
...