OGeek|极客世界-中国程序员成长平台

标题: ios - UITableViewRowAction - 一个的背景颜色影响另一个 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:21
标题: ios - UITableViewRowAction - 一个的背景颜色影响另一个

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

- (NSArray<UITableViewRowAction *> *)tableViewUITableView *)tableView
                  editActionsForRowAtIndexPathNSIndexPath *)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 条件中,一个按钮被创建为禁用。

然而,messageActionbackgroundColor 会影响其他两个,当只有messageAction 被启用而其他两个被禁用时。

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

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



Best Answer-推荐答案


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

-(NSArray *)tableViewUITableView *)tableView editActionsForRowAtIndexPathNSIndexPath *)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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4