Итак, я хочу иметь возможность определить, как отличить операции вставки от операций удаления, чтобы я мог ответить соответствующим образом. В настоящее время у меня есть этот код для создания кнопок «Готово», «Редактировать» и «Добавить»
- (void)initializeNavigationBarButtons
{
UIBarButtonItem *newEditButton =
[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self action:@selector(performEdit:)];
self.editButton = newEditButton;
[newEditButton release];
self.navigationItem.rightBarButtonItem = self.editButton;
UIBarButtonItem *newDoneButton =
[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:@selector(performDone:)];
self.doneButton = newDoneButton;
[newDoneButton release];
UIBarButtonItem *newAddButton =
[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(performAdd:)];
self.addButton = newAddButton;
[newAddButton release];
self.navigationItem.leftBarButtonItem = self.addButton;
}
тогда у меня есть эти 3 как так называемые функции "обратного вызова" для кнопок:
- (void)performDone:(id)paramSender
{
[self.tableView setEditing:NO animated:YES];
[self.navigationItem setRightBarButtonItem:self.editButton
animated:YES];
[self.navigationItem setLeftBarButtonItem:self.addButton
animated:YES];
}
- (void)performEdit:(id)paramSender
{
NSLog(@"Callback Called");
[self.tableView setEditing:YES animated:YES];
[self.navigationItem setRightBarButtonItem:self.doneButton
animated:YES];
[self.navigationItem setLeftBarButtonItem:self.doneButton
animated:YES];
}
- (void)performAdd:(id)paramSender
{
[self.tableView setEditing:YES animated:YES];
[self.navigationItem setRightBarButtonItem:self.doneButton
animated:YES];
[self.navigationItem setLeftBarButtonItem:self.doneButton
animated:YES];
}
и вот где я «должен» определить, является ли это операцией добавления или удаления:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *output = (isDeleting) ? @"Deleting" : @"Adding";
NSLog(@"%@", output);
UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;
if ([tableView isEqual:self.tableView]){
if (self.isDeleting == YES){
result = UITableViewCellEditingStyleDelete;
}
else{
result = UITableViewCellEditingStyleInsert;
}
}
return result;
}
однако, я не знаю, где я должен установить self.isDeleting и self.isAdding. Я попытался установить их в обратных вызовах, но кажется, что tableView: cellEditingStyleForRowAtIndexPath: вызывается первым, и в моем viewDidLoad значение по умолчанию для них - NO.
Так как мне правильно установить значения isAdding и isDeleting, чтобы я мог действовать соответствующим образом в tableView: cellEditingStyleForRowAtIndexPath: method?
Заранее спасибо!