maddie
maddie

Reputation: 1954

How to set the action of UITableViewRowAction

I customized my UITableCellDeleteConfirmationView to change the background color of the button in iOS 8 and above. Following this post, I implemented editActionsForRowAtIndexPath, commitEditingStyle and canEditRowAtIndexPath.

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

    RankedSpecies* rankedSpecies = [fetchedResultsController objectAtIndexPath:indexPath];

    if ( [self.collectedLeaf.selectedSpecies isEqualToString:[rankedSpecies.Species commonNameFirstLast]] )
    {
        UITableViewRowAction *unlabelSpecies = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"UnLabel" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                        {
                                             NSLog(@"Action to perform with Button 1");
                                        }];
        unlabelSpecies.backgroundColor = [UIColor darkGrayColor];

        return @[unlabelSpecies];
    }

    UITableViewRowAction *labelSpecies = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Label" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                    {
                                        NSLog(@"Action to perform with Button 1");
                                    }];
    labelSpecies.backgroundColor = [UIColor darkGrayColor];

    return @[labelSpecies];
}

However, when I push these custom buttons, they do not call commitEditingStyle. According to this post, commitEditingStyle is only fired when you touch the delete button.

Instead of trying to figure out how to fire commitEditingStyle, I have created methods that replicate my commitEditingStyle implementation, removeCollectedLeafLabel. Can a class method be an action? How do I set the UITableViewRowAction to do something when pressed?

Upvotes: 0

Views: 250

Answers (1)

Kuntal Gajjar
Kuntal Gajjar

Reputation: 802

when your press button it will call the block.

UITableViewRowAction *unlabelSpecies = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"UnLabel" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                        {
                                             CALL_YOUR_METHOD_FROM_HERE
                                        }];


unlabelSpecies.backgroundColor = [UIColor darkGrayColor];

return @[unlabelSpecies];

Upvotes: 1

Related Questions