2

I have 3 UITableViewRowAction's in my source code, like below:

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

In each if condition, a button is created as enabled, whereas in the respective else condition, as disabled.

However, the backgroundColor of messageAction affects the other two when only messageAction is enabled and other two are disabled.

To confirm this, I reversed the buttons display order by putting cancelAction as first. That way, cancel button's backgroundColor affected other two.

How can I fix the visualization od backgroundColor property of each button?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Nitish
  • 13,845
  • 28
  • 135
  • 263
  • what's the desired behavior? it is not clear – ddb Aug 24 '16 at 10:52
  • @ddb : Desired behaviour is to show the buttons in disabled form (by lightening their background) when they don't have the permission. – Nitish Aug 24 '16 at 10:56
  • @ddb : But if it would be only one object, then how do you suggest to display 3 different buttons ? – Nitish Aug 24 '16 at 11:04
  • @ddb : You deleted your answer !! – Nitish Aug 24 '16 at 11:28
  • Yes, it is not correct, eventally I'll write a new one ;) sorry, but I do not want to receive down-votes for nothing – ddb Aug 24 '16 at 11:29
  • (just guessing, as your code seems correct after you explained to me better) may you try to split and exec the `addObject:` statements just after each specific if-else branch? – ddb Aug 24 '16 at 11:43
  • Try to use `ternary operator` rather than if-else condition. – pkc456 Aug 24 '16 at 11:51

2 Answers2

0

Override the editActionsForRowAtIndexPath tableview delegate method as follows:-

-(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];
}
pkc456
  • 8,350
  • 38
  • 53
  • 109
0

Your background colours have alpha values - The action views are drawn overtop of each other from the rightmost button inwards.

Use a colour with no alpha and it should be fine and not blend with the other button background colours.

Ryan
  • 5,416
  • 1
  • 39
  • 36