我自定义了UITableCellDeleteConfirmationView以更改iOS 8及更高版本中按钮的背景颜色。 Following this post,我实现了editActionsForRowAtIndexPathcommitEditingStylecanEditRowAtIndexPath

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


但是,当我按下这些自定义按钮时,它们不会调用commitEditingStyleAccording to this postcommitEditingStyle仅在您触摸删除按钮时才会触发。

我没有尝试找出如何触发commitEditingStyle的方法,而是创建了复制commitEditingStyle实现removeCollectedLeafLabel的方法。类方法可以作为动作吗?如何设置UITableViewRowAction在被按下时执行某些操作?

最佳答案

当您按下按钮时,它将调用该块。

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


unlabelSpecies.backgroundColor = [UIColor darkGrayColor];

return @[unlabelSpecies];

08-05 22:10