2008-12-11 80 views
13

我很好奇,如果有可能在UITableView上攔截'編輯'模式的默認方法。通常,如果您滑動具有與之關聯的委託方法的UITableViewCell,您將獲得一個免費的「刪除」按鈕。我想將刪除更改爲其他任意選擇器。我不想刪除單元格,而只想運行hello world alert對話。這是可能的嗎?UITableView,截取編輯模式

回答

3

UITableViewCell上有一個名爲editAction的屬性,它記錄爲允許您更改用於插入或刪除單個單元格的操作(它也使用單元格的target屬性)。我沒有測試過它,但是這聽起來像它可能做你想要的。

+3

從iOS 3.0開始,此功能已被棄用。 – 2011-10-08 16:40:26

0

我會實現一個UITableViewCell的子類,並在那裏處理觸摸事件。你可能必須自己做所有的動畫,但我認爲這可能是最簡單的解決方案。沒有「支持」方法來更改刪除按鈕,我不認爲

21

編輯是作爲您的UITableView的委託對象上的方法實現的。在你的桌子控制器,擁有的任何控制激活編輯電話本:

[tableView setEditing: YES animated: YES]; 

然後,請確保您的代理對象實現這一點:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
      UIAlertView *alert = [[UIAlertView alloc] 
       initWithTitle: @"Delete" 
       message: @"Do you really want to delete 「George W. Bush」?" 
       delegate: self 
       cancelButtonTitle: @"Cancel" 
       otherButtonTitles: @"Of course!", nil]; 
    } 
} 

...或更標準的行動可能是:

[itemList removeObjectAtIndex:indexPath.row]; 
[table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; 
+1

問題是用戶點擊「取消」時該怎麼做。我所做的唯一一件事情就是`reloadRowsAtIndexPaths`來擺脫刪除按鈕。 – 2010-07-28 02:41:02

6

@JFMartin和Marco - 使用下面的UITableview代理方法替換標準的「刪除」按鈕

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath