2011-09-10 34 views
0

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

在此先感謝!

回答

1

如果您在導航欄中有一個'添加'按鈕,爲什麼不在按下時運行'add'操作而不是使tableview可編輯?當單元格具有內容時,將單元格的編輯樣式設置爲UITableViewCellEditingStyleInsert也沒有多大意義。通常的流程是在表格外部有一個「添加」按鈕(例如:在導航欄中)執行添加操作,或者在編輯樣式時作爲tableview中的最後一個(或第一個)單元格,以及單元格被按下,添加動作被執行。因此,您可以在導航欄中保留'添加'按鈕並使所有單元格可編輯爲UITableViewCellEditingStyleDelete,或者只保留'編輯'/'完成'按鈕,並在編輯時添加新單元格(而tableview爲可編輯),可編輯UITableViewCellEditingStyleInsert

注意:您應該更好地使用if (tableView == self.tableView)而不是if ([tableView isEqual:self.tableView]),因爲您想檢查它是否是相同的實例,而不是它們是否相等。

相關問題