2009-11-21 35 views
12

對於我的iPhone應用程序,我有一個可編輯的(用於刪除)表格視圖。我希望能夠檢測到用戶點擊了「編輯」按鈕。看到這個圖片:http://grab.by/It0如何檢測iPhone上的編輯模式UITableView

從文檔,它看起來像如果我執行:

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath 

那麼我可以檢測到它(儘管從方法的名字,我也不會覺得)。這證明不起作用。

任何想法檢測到這?我想要的原因是我想在刪除模式下連接左上角的「全部刪除」按鈕。

感謝

回答

19

它可能不是您正常工作,因爲willBeginEditingRowAtIndexPath:被稱爲編輯開始之前

如果您要檢查,而在另一種方法,你需要的editing屬性:

@property(nonatomic, getter=isEditing) BOOL editing 

如果你想要做的事,當按下「編輯」按鈕,你需要實現setEditing方法:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated 

您可以在UIViewController中找到。 (嗯,這是最有可能的地方;還有其他地方。)

+0

(無效)setEditing:(BOOL)編輯動畫:(BOOL)動畫{[超級setEditing:編輯動畫:動畫]。 }爲編輯按鈕保留其預期的操作。 – 2013-02-11 19:23:45

3

該方法告訴你何時用戶正在編輯單元格,而不是將表格置於編輯模式。有一種進入編輯模式時調用,要求每個細胞的方法,如果它可以編輯:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 

我不認爲重寫setEditing:animated:是有道理的,因爲你將不得不繼承UITableView這是額外的工作和一個你沒有其他原因需要的課程,更不用說它將不得不傳達一個事實,即編輯已經被打開回控制器。其他

一種選擇是簡單地將自己添加編輯按鈕 - 這是在UIBarButtonSystemItem內置,您可以添加它,然後把它調用自己的方法,其中你做一些具體的事情則呼籲UITableView本身setEditing:animated:

編輯背後的想法是,當啓用編輯時,每個單元格被告知進入編輯模式,並詢問是否有任何特定的編輯控件應該應用。所以理論上,除了改變單元格的外觀之外,不需要檢測進入編輯模式。進入編輯模式時你想做什麼?

+0

setEditing位於UITableViewController上,你可能已經有了子類,而不是在tableview本身。至於你可能想要這樣做的一個原因,可以說用戶可以對行進行重新排序,但是你不想把這個重新排序提交到web服務器,直到完成所有的操作。 – Nathan 2010-10-28 08:32:07

13

當子類化一個tableviewcontroller(大多數人大部分時間都會做,因爲你必須重寫它的委託方法只是把數據放入它...),你可以重寫setEditing:animated:方法來抓取編輯狀態的變化。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    NSLog(@"Editing %i", editing); 
    [super setEditing:editing animated:animated]; 
} 

這一起傳遞給超類的狀態變化,但可讓您在中間跳和檢測的改變,或改變它,如果你想...

+1

[super setEditing:editing animated:animated];編輯按鈕需要保留其預期的操作。 – 2013-02-11 19:21:54

3

肯德爾的答案作品。我以下面的方式做了。

// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the specified item to be editable. 
    NSLog(@"Can edit %d", tableView.editing); 
    if (tableView.editing == 1) { 
     [self.editButtonItem setTitle:EDIT_BUTTON_TITLE]; 
    }else { 
     [self.editButtonItem setTitle:DONE_BUTTON_TITLE]; 
    } 

    return YES; 
} 
3

的setEditing:動畫:例子並沒有爲我工作(在iOS 6.1),以檢測當你進入和退出刪除確認模式所發生的狀態變化。看起來setEditing:animated:僅在表視圖進入編輯模式時調用一次,而不是單元格的狀態更改。在一些調試器有趣之後,我到達了一種檢測單元狀態變化的方法。

我的用例與您的不同。我只是想在顯示刪除按鈕時隱藏標籤,以便其他單元格內容在「刪除」按鈕滑入時不會與其重疊(我使用的是UITableViewCellStyleValue2,左側爲藍色標籤,黑色標籤爲UITableViewCellStyleValue2右側)

(在你的UITableViewCell子類)

- (void)willTransitionToState:(UITableViewCellStateMask)state { 
    [super willTransitionToState:state]; 
    if (state & UITableViewCellStateShowingDeleteConfirmationMask) { 
     // showing delete button 
     [self.textLabel setAlpha:0.0f]; // <-- I just wanted to hide the label 
    } 
} 

- (void)didTransitionToState:(UITableViewCellStateMask)state { 
    if (!(state & UITableViewCellStateShowingDeleteConfirmationMask)) { 
     // not showing delete button 
     [self.textLabel setAlpha:1.0f]; // <-- show the label 
    } 
}