2014-03-14 23 views
1

我想改變按鈕的顏色和文字時UITableViewCellEditingStyleDelete,我該怎麼做?以下是我如何調用刪除單元格的方法:)謝謝!如何在UITableViewCellEditingStyleDelete時自定義刪除按鈕?

(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if(editingStyle == UITableViewCellEditingStyleDelete){ 
     [self.tasks removeObjectAtIndex:indexPath.row]; 
     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
     [userDefaults setObject:self.tasks forKey:@"tasks"]; 
     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; 
     [tableView endUpdates]; 
    } 
} 
+0

查看「UITableViewDelegate」的文檔,瞭解可以實現以提供不同標籤的方法。沒有標準的方法來改變顏色。 – rmaddy

回答

5

你可以做的是:

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //Set NSString for button display text here. 
    NSString *newTitle = @"Remove"; 
    return newTitle; 

} 

方法調用要做到這一點在Apple iOS Dev Docs here

老實說,我不會改變顏色 - Apple HIG(人機界面指南)可能會建議顏色保持一致的紅色,以確保用戶對您的應用程序的行爲沒有不同的期望。

0

您可以自定義您的手機。然後,在方法的cellForRowAtIndexPath添加UISwipeGestureRecognizer該單元是這樣的:

//custom delete button on cell comment 
UISwipeGestureRecognizer * swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellWasSwiped:)]; 
[swipeRecognizer setDirection:(UISwipeGestureRecognizerDirectionLeft | 
            UISwipeGestureRecognizerDirectionRight)]; 
[cell addGestureRecognizer:swipeRecognizer]; 

實現cellWasSwiped功能:

- (void)cellWasSwiped:(UISwipeGestureRecognizer *)recognizer { 
//edit your custom button delete in here. 
..... 
[self.tableView commitEditingStyle:UITableViewCellEditingStyleDelete forRowAtIndexPath:indexPath]; 
} 
+0

我不能在選擇器上實現commiteditingStyle:S –

相關問題