UITableViewCell滑动行或点击编辑按钮时,如何更改默认的红色删除按钮的颜色?

38

我希望在编辑按钮被点击或滑动UITableView行时,更改UITableViewCell的减号按钮和删除按钮的颜色。我已经实现了以下代码:

-(IBAction)doEdit:(id)sender
{

    [[self keyWordsTable] setEditing:YES animated:NO];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

}

请查看以下链接:https://dev59.com/vnI-5IYBdhLWcg3w6dC2 - Ilario
4
似乎您从未得到如何更改实际减号按钮颜色的答案,只能更改确认删除按钮的颜色。 - SAHM
5个回答

101

iOS 8和9感谢此帖子


注意:如果您正在使用现有的iOS 7项目,则需要将目标更新到iOS 8才能获得此功能。还要记得设置UITableviewDelegate。

所有魔法现在都发生在这里(想要多少按钮都可以!!!):

 -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
 UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
    {
        NSLog(@"Action to perform with Button 1");
    }];
    button.backgroundColor = [UIColor greenColor]; //arbitrary color
    UITableViewRowAction *button2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                    {
                                        NSLog(@"Action to perform with Button2!");
                                    }];
    button2.backgroundColor = [UIColor blueColor]; //arbitrary color

    return @[button, button2];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// you need to implement this method too or nothing will work:

}
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES;
    }

(iOS 7)


**activate the delete button on swipe**

// make sure you have the following methods in the uitableviewcontroller

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES;
    }
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"You hit the delete button.");
    }

设置自定义文本标签而不是删除。

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"Your Label";
}

设置自定义颜色的按钮部分1-警告,这在技术上涉及到篡改私有的苹果 API。但是,您可以使用 UIKIT 的公共方法搜索修改子视图。

创建一个uitableviewcell类(也可以参考https://dev59.com/LWfWa4cB1Zd3GeqPjrUq#22350817

- (void)layoutSubviews
{
    [super layoutSubviews];
    for (UIView *subview in self.subviews) {
        //iterate through subviews until you find the right one...
        for(UIView *subview2 in subview.subviews){
            if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
                //your color
                ((UIView*)[subview2.subviews firstObject]).backgroundColor=[UIColor blueColor];
            }
        }
    }    
}

另外需要注意的是,并不能保证这种方法在未来的更新中仍能正常使用。同时请注意,提及或使用私有的UITableViewCellDeleteConfirmationView类可能会导致AppStore拒绝。

为按钮设置自定义颜色-第二部分

回到你的UITableViewController

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    [YourTableView reloadData];
}

在下一次调用tablecell的layoutSubviews之前,备用颜色不会被调用,因此我们通过重新加载所有内容来确保这一点。


这个能在iOS 7.2、7.3、8.0b1和8.0b3上运行吗?另外,在你的代码中使用UITableViewCellDeleteConfirmationView会导致你的应用被拒绝。 - Léo Natan
从6到7唯一的变化是命名约定——从UITableViewCellDeleteConfirmationControl更改为UITableViewCellDeleteConfirmationView。我除了查找视图并更改其背景之外什么也没做。许多应用程序以前都使用过这种技术。如果苹果公司说不行,对我来说也不是什么大问题。其余的技术都是合法的,并且在文档中有说明:)。无论如何,我已经尝试过其他解决方案,例如SWTableCells,但与内置的苹果按钮相比,精度不够。我只是不想因为应用程序的颜色方案而有一个红色按钮。 - timothykc
你应该至少尝试隐藏私有API的使用。此外,iOS6和iOS7表格视图单元格之间的私有视图层次结构发生了重大变化。你的子视图遍历虽然现在可以工作,但可能会在未来的每个版本中出现问题。但是,在你的帖子中最糟糕的是公然使用私有API。 - Léo Natan
1
感谢反馈和警告。尽管我会无视这些建议,但我还是很感激。我将编辑答案,以便其他人明白这是“买者自负”。至于它是否会在未来出现问题,唯一真正的损失就是颜色。如果我必须编写自定义解决方案,那也没关系 :) - timothykc
1
请注意,您不再需要实现“commitEditingStyle”,而是调用UITableViewRowAction的处理程序。 - Ashley Mills
显示剩余4条评论

23

Swift示例(iOS 8)

UITableViewDelegate文档(editActionsForRowAtIndexPath方法)

返回值

一个包含UITableViewRowAction对象的数组,表示行的操作。您提供的每个操作都用于创建用户可以点击的按钮。

讨论

当您希望为表格的某个行提供自定义操作时,请使用此方法。当用户在行上水平滑动时,表格视图将行内容移开以显示您的操作。点击其中一个操作按钮会执行存储在操作对象中的处理程序块。

如果您不实现此方法,则在用户滑动行时,表视图会显示标准附加按钮。

在Swift中的工作示例:

@available(iOS 8.0, *)
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let button1 = UITableViewRowAction(style: .Default, title: "Happy!") { action, indexPath in
        print("button1 pressed!")
    }
    button1.backgroundColor = UIColor.blueColor()
    let button2 = UITableViewRowAction(style: .Default, title: "Exuberant!") { action, indexPath in
        print("button2 pressed!")
    }
    button2.backgroundColor = UIColor.redColor()
    return [button1, button2]
}

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}

2
还有减号按钮(这也被问到了)? - User

3

当你在 .m 文件(自定义单元格)中第一次调用 willTransitionToState 方法时:

- (void)willTransitionToState:(UITableViewCellStateMask)state{
    NSLog(@"EventTableCell willTransitionToState");
    [super willTransitionToState:state];
    [self overrideConfirmationButtonColor];
}

检查iOS版本,这里是,我正在使用iOS 7 - iOS 8

//at least iOS 8 code here
- (UIView*)recursivelyFindConfirmationButtonInView:(UIView*)view
{
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) {
        // iOS 8+ code here
        for(UIView *subview in view.subviews) {

            if([NSStringFromClass([subview class]) rangeOfString:@"UITableViewCellActionButton"].location != NSNotFound)
                return subview;

            UIView *recursiveResult = [self recursivelyFindConfirmationButtonInView:subview];
            if(recursiveResult)
                return recursiveResult;
        }
    }

    else{
        // Pre iOS 8 code here
        for(UIView *subview in view.subviews) {
            if([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationButton"]) return subview;
            UIView *recursiveResult = [self recursivelyFindConfirmationButtonInView:subview];
            if(recursiveResult) return recursiveResult;
        }
    }
    return nil;


}

-(void)overrideConfirmationButtonColor
{

    dispatch_async(dispatch_get_main_queue(), ^{
        UIView *confirmationButton = [self recursivelyFindConfirmationButtonInView:self];
        if(confirmationButton)
        {
            UIColor *color = UIColorFromRGB(0xFF7373);
            confirmationButton.backgroundColor = color;

        }
    });
}

2

公共API无法实现此功能。

对于删除按钮,您可以使用自定义实现,例如SWTableViewCell,来更改按钮的颜色,并添加其他按钮。


它在iOS 8.2中(请查阅UITableViewDelegate文档)。 - clearlight
@binarystar 旧答案。 - Léo Natan

1

虽然这是一个老问题,但我相信仍有一些人支持iOS 7。如果想要改变删除按钮的背景颜色,你需要创建或扩展 "UITableViewCell" 类。然后可以使用以下代码:

- (void)layoutSubviews
{
    [super layoutSubviews];
    for (UIView *subview in self.subviews) {
        for(UIView *childView in subview.subviews){
            if ([childView isKindOfClass:[UIButton class]]) {
                childView.backgroundColor = [UIColor blueColor];
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接