2010-11-13 23 views
1

在我的應用程序中,我需要使用UIViewController來控制我的tableView而不是UITableViewController(因爲我需要在我的tableView下面的額外視圖)。我已經成功實現了視圖控制器(以及委託和數據源),但是現在我需要在用戶在單元格上從左到右滑動時在我的UITableViewCell上獲得正確的行爲。模仿UITableViewControllers行爲tableView:willBeginEditingRowAtIndexPath:在UIViewController

當用戶點擊編輯按鈕(顯示重排拖動手柄,以及使刪除確認顯示的小「 - 」按鈕)時所需的行爲很容易做到。我只是不喜歡這在我的視圖控制器:

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

但我無法弄清楚如何使刪除確認顯示,如果用戶從左至右細胞刷卡。這是我在tableView:willBeginEditingRowAtIndexPath:已經得到了現在:

- (void)tableView:(UITableView *)aTableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath { 
// Tell the cell to do its custom animations 
[[self.tableView cellForRowAtIndexPath:indexPath] setEditing:YES animated:YES]; 

// set the viewControllers editing to YES, thus changing the editButton to a doneButton 
self.editing = YES; 
} 

有了這個代碼,它應該和細胞做它的動畫騰出空間刪除確認的editButton變爲doneButton,但刪除確認本身不顯示。

我的問題:如何在指定的tableviewCell上顯示刪除確認?

回答

0

我想出了自己。實際上,我所要做的就是將我的setEditing:animated中的相同代碼插入到tableView:willBeginEditingRowAtIndexPath方法中。

- (void)tableView:(UITableView *)aTableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath { 
    [super setEditing:isEditing animated:animated]; 
    [self.tableView setEditing:isEditing animated:animated]; 

    //Self.editing handles the done/edit button 
    self.editing = YES; 
} 

這樣做的伎倆。我不知道tableView如何知道它只能顯示一個單元格的刪除按鈕,但它確實有效。

+0

注意:如果您希望編輯按鈕返回到完成狀態(如果用戶使刪除按鈕消失(不按下done按鈕)),則應該只實現tableView:didEndEditingRowAtIndexPath:其中您設置了self.editing = NO – Sorig 2010-11-14 12:24:56

2

確保您實現方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

時,這是實現(它需要無論如何,因爲這是你處理刪除按鈕)時,刷卡行顯示的刪除按鈕手勢會自動爲您執行。

+1

我已經實現了這個方法,但它並沒有顯示刪除按鈕,據我所見。當用戶點擊刪除按鈕時會調用此方法,由於不顯示刪除按鈕,用戶無法刪除該按鈕。 – Sorig 2010-11-14 11:26:02