简体   繁体   中英

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?

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM