滑动行或单击编辑按钮时更改 UITableViewCell 中默认红色删除按钮的颜色 [英] Change the color of default red color delete button in UITableViewCell when swiping rows or click on edit button

查看:45
本文介绍了滑动行或单击编辑按钮时更改 UITableViewCell 中默认红色删除按钮的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击编辑按钮或滑动 UITableView 行时更改 UITableViewCell 的减号按钮和删除按钮的颜色.到目前为止,我已经实现了这段代码:

I wanted to change the color of minus button and delete button of UITableViewCell when click on edit button or swiping UITableView rows. I have implemented this code so far :

-(IBAction)doEdit:(id)sender
{

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

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

}

推荐答案


iOS 8 和 9(这篇文章的道具)

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

Note: If you are working with an existing iOS 7 project, you'll need to update the target to iOS 8 to get this functionality. Also remember to set the UITableviewDelegate.

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

All the magic now happens here (as many buttons as you want too!!!!):

 -(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 部分设置自定义颜色 - 警告,这在技术上涉及对私有 Apple API 的探查.但是,不会阻止您使用作为 UIKIT 一部分的公共方法搜索来修改子视图.

创建一个 uitableviewcell 类(另见 https://stackoverflow.com/a/22350817/1758337)>

Create a uitableviewcell class (see also https://stackoverflow.com/a/22350817/1758337 )

- (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 被拒绝.

Another note: there's no guarantee this approach will work in future updates. Also beware that mentioning or using the private UITableViewCellDeleteConfirmationView class may lead to AppStore rejection.

为按钮部分 2 设置自定义颜色

回到你的 uitableviewcontroller

back in your uitableviewcontroller

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

(直到下次在 tablecell 上调用 layoutSubviews 时才会调用替代颜色,因此我们通过重新加载所有内容来确保发生这种情况.)

(The alternate color won't be called until the next time layoutSubviews is called on the tablecell, so we ensure this happens by reloading everything.)

这篇关于滑动行或单击编辑按钮时更改 UITableViewCell 中默认红色删除按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆