因此,我希望能夠確定如何區分插入操作和刪除操作,以便我可以做出相應的響應。目前,我有這樣的代碼來創建一個「完成」,「編輯」和「添加」按鈕確定編輯操作是刪除還是在UITableViewController中添加
- (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:方法中相應地採取行動?
在此先感謝!