如何在UITableViewCellEditingStyleDelete时自定义删除按钮?

时间:2014-03-14 01:16:22

标签: ios uitableview

我想在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];
    }
}

2 个答案:

答案 0 :(得分:6)

您可以做的是:

-(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(人机界面指南)可能会建议颜色保持一致的红色,以确保用户对您应用的行为没有不同的期望。

答案 1 :(得分: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];
}