简体   繁体   English

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

[英]How to customize the delete button when UITableViewCellEditingStyleDelete?

I want to change the color and the text of the button when UITableViewCellEditingStyleDelete , how can I do that? 我想在UITableViewCellEditingStyleDelete时更改按钮的颜色和文本,我该怎么做? Here's how I call the method for deleting a cell :) Thanks! 这是我调用删除单元格的方法:)谢谢!

(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];
    }
}

What you can do is the following: 您可以执行以下操作:

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

}

The method call to accomplish this is found in the Apple iOS Dev Docs here 可在此处Apple iOS开发文档中找到实现此目的的方法调用

Honestly, I would NOT change the color - the Apple HIG (Human Interface Guidelines) would likely recommend the color stay a consistent red to make sure users don't have a different expectation for your app's behavior. 老实说,我不会更改颜色-Apple HIG(人机界面指南)可能会建议颜色保持一致的红色,以确保用户对应用程序的行为没有不同的期望。

You can custom your cell. 您可以自定义您的单元格。 And then, add UISwipeGestureRecognizer for this cell on method cellforRowatindexPath like this: 然后,像这样在方法cellforRowatindexPath上为此单元格添加UISwipeGestureRecognizer:

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

Implement cellWasSwiped function: 实现cellWasSwiped函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM