ios - UITableViewRowAction - 一个的背景颜色会影响另一个

标签 ios objective-c uitableview uitableviewrowaction

我的源代码中有 3 个 UITableViewRowAction 的,如下所示:

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView
                  editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(messagePremission)
    {
        messageAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:[NSString stringWithFormat:@"%@%@", NSLocalizedString(@"message_admin", nil), activity.GroupName] handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){

                }];

        messageAction.backgroundColor = [UIColor colorWithRed:82.0/255.0 green:82.0/255.0 blue:82.0/255.0 alpha:1.0];
    }
    else
    {
        messageAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:[NSString stringWithFormat:@"%@%@", NSLocalizedString(@"message_admin", nil), activity.GroupName] handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){

                }];

        messageAction.backgroundColor = [UIColor colorWithRed:200.0/255.0 green:199.0/255.0 blue:205.0/255.0 alpha:0.1];
    }

    if(editPermission)
    {
        editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:NSLocalizedString(@"edit_swipe", nil) handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){

            }];

        editAction.backgroundColor = [UIColor colorWithRed:2.0/255.0 green:118.0/255.0 blue:246.0/255.0 alpha:1.0];
    }
    else
    {
        editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:NSLocalizedString(@"edit_swipe", nil) handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){

            }];

        editAction.backgroundColor = [UIColor colorWithRed:2.0/255.0 green:118.0/255.0 blue:246.0/255.0 alpha:0.1];
    }
    if(cancelPermission)
    {
        cancelAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Cancel"  handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){   
            }];

        cancelAction.backgroundColor = [UIColor colorWithRed:251.0/255.0 green:1.0/255.0 blue:13.0/255.0 alpha:1.0];
    }
    else
    {
        cancelAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Cancel"  handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){   
            }];

        cancelAction.backgroundColor = [UIColor colorWithRed:251.0/255.0 green:1.0/255.0 blue:13.0/255.0 alpha:0.1];
    }  

    [arrButtons addObject:messageAction];
    [arrButtons addObject:editAction];
    [arrButtons addObject:cancelAction]; 

    return arrButtons;
}

在每个 if 条件下,按钮被创建为启用,而在相应的 else 条件下,按钮被禁用。

但是,当仅启用messageAction 并禁用其他两个时,messageActionbackgroundColor 会影响其他两个。

为了确认这一点,我通过将 cancelAction 放在第一位来颠倒按钮显示顺序。这样,取消按钮的 backgroundColor 会影响其他两个。

如何修复每个按钮的可视化 od backgroundColor 属性?

最佳答案

覆盖editActionsForRowAtIndexPath tableview 委托(delegate)方法如下:-

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *messageButton = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Message" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                    {
                                        NSLog(@"Action to perform with messageButton Button");
                                    }];
    UIColor *messageColor  = if(messagePremission == true) ? [UIColor colorWithRed:82.0/255.0 green:82.0/255.0 blue:82.0/255.0 alpha:1.0] :
        [UIColor colorWithRed:200.0/255.0 green:199.0/255.0 blue:205.0/255.0 alpha:0.1];
    messageButton.backgroundColor = messageColor;

    UITableViewRowAction *editButton = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                     {
                                         NSLog(@"Action to perform with editButton Button!");
                                     }];
    UIColor *editColor  = if(editPermission == true) ? [UIColor colorWithRed:2.0/255.0 green:118.0/255.0 blue:246.0/255.0 alpha:1.0] :
        [UIColor colorWithRed:2.0/255.0 green:118.0/255.0 blue:246.0/255.0 alpha:0.1];
    editButton.backgroundColor = editColor;

    UITableViewRowAction *cancelButton = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Cancel" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                     {
                                         NSLog(@"Action to perform with cancelButton");
                                     }];
    UIColor *cancelColor  = if(cancelPermission == true) ? [UIColor colorWithRed:251.0/255.0 green:1.0/255.0 blue:13.0/255.0 alpha:1.0] :
        [UIColor colorWithRed:251.0/255.0 green:1.0/255.0 blue:13.0/255.0 alpha:0.1];
    cancelButton.backgroundColor = cancelColor;

    return @[messageButton, editButton, cancelButton];
}

关于ios - UITableViewRowAction - 一个的背景颜色会影响另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39121250/

相关文章:

iphone - 带有标签栏的半透明覆盖

ios - UITableViewCell 子类?

ios - 如何添加动态部分但仍然只添加到其中一个部分?

ios - 如何在不使用 nib 名称的情况下加载自定义 UITableViewCell

ios - 如何将C/C++代码移植到iOS中?

ios - 在 iCarousel 控件中在图像上显示文本

iphone - 在 NSArray 中访问 NSDictionary

iphone - 正确释放一个 viewController,将其自身设置为其他类的委托(delegate)?

ios - Appium 医生显示错误

objective-c - tvOS 10. PreferredFocusView 已弃用。如何更改为 preferredFocusEnvironment?