2011-09-04 53 views
2

我已經有UITableView已經編程創建。我現在試圖添加使用編輯模式刪除行的功能。iOS「刷卡刪除」不改變編輯按鈕狀態

我添加了這樣的默認方式:

self.navigationItem.leftBarButtonItem = self.editButtonItem; 

當你輕點按鈕,進入編輯模式,按鈕變爲「完成」按鈕。

但是,當我「滑動刪除」行時,只顯示刪除按鈕,現有編輯按鈕不會更改爲「完成」。

有什麼我需要做額外的,因爲我以編程方式創建表視圖?

下面是我的實現代碼如下代碼:

- (void)viewDidLoad 
{ 

    self.voucherTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 36, 320, self.view.frame.size.height - 36) style:UITableViewStylePlain]; 
    self.voucherTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 
    self.voucherTableView.delegate = self; 
    self.voucherTableView.dataSource = self; 
    [self.view addSubview:self.voucherTableView]; 

    self.navigationItem.rightBarButtonItem = self.editButtonItem; 

} 

// Default table view data source methods go here 

- (void)setEditing:(BOOL)editing animated:(BOOL)animated 
{ 
    [super setEditing:editing animated:animated]; 
    [self.voucherTableView setEditing:editing animated:animated]; 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewCellEditingStyleDelete; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     // Commit the delete 
    } 
} 
+0

我對你的問題有點困惑。通常使用左側的刪除按鈕或通過滑動/滑動刪除來完成刪除。無論哪種情況,你都應該得到一個「刪除」按鈕來確認。 「完成」按鈕與刪除無關,只是退出編輯模式。查看tableview的完整設置也很有幫助。 –

+0

感謝您的評論。對於這兩種類型的編輯,我都得到了「刪除」按鈕,但是當您執行「幻燈片刪除」時,原始的「編輯」按鈕不會更改爲「完成」。我已將我的代碼添加到原始問題中。 –

+0

好多了! –

回答

3

這是我的答案:該按鈕不會更改爲「完成」,因爲要刪除的幻燈片實際上不必在「編輯」模式下完成,並且不會讓您進入編輯模式。

如果你希望當你執行刪除時,你可以強制它進入編輯模式,不知道你爲什麼要這樣做?

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [tableView setEditing:YES animated:YES]; 
     // Commit the delete 
    } 
} 
+0

好的,我沒有意識到這並不意味着。我認爲我必須假定,因爲在「消息」應用程序中,如果您「幻燈片刪除」它將更改編輯按鈕完成,顯然他們必須手動執行此操作。謝謝你的幫助。 –

+0

是的,它有點不一致。很高興幫助:) –

+0

Apple的文檔爲'-tableView:commitEditingStyle:forRowAtIndexPath:'特別說**「你不應該在這個方法的實現中調用setEditing:animated:如果由於某種原因你必須在延遲後調用它通過使用performSelector:withObject:afterDelay:方法。「** –

0

@Scrooby:編輯按鈕標題和狀態也不會改變爲「完成」

這是因爲「編輯」按鈕,點擊觸發編輯模式的tableview,但是tableview的編輯模式不會觸發事件來將按鈕標題改爲「完成」。

假設編輯按鈕單擊(事件A)&的TableView在編輯模式(事件B)

事件A指事件B,但事件B並不意味着事件A.

希望你得到它現在。這是一個簡單的邏輯。

希望這可以幫助你。

0

嘗試將[super setEditing:editing animated:animated];改爲放在方法的末尾。它應該工作。

1

的新功能,電流的iOS 8

只要你使用self.editingItem它現在應該對自己的正確更新。實際上,-tableView:commitEditingStyle:forRowAtIndexPath:的Apple文檔現在特別指出:

您不應該在此方法的實現中調用-setEditing:animated:。如果出於某種原因,您必須在延遲後使用-performSelector:withObject:afterDelay:方法調用它。

這是從其他答案中提到的上一個功能的改變。

需要注意的一點是,截至iOS 8.1。2,如果您未在-tableView:commitEditingStyle:forRowAtIndexPath:中調用-deleteRowsAtIndexPaths:withRowAnimation:(例如,在更新數據模型之後調用[self.tableView reloadData]),編輯項將停留在編輯狀態。