简体   繁体   English

如何设置UITableViewRowAction的动作

[英]How to set the action of UITableViewRowAction

I customized my UITableCellDeleteConfirmationView to change the background color of the button in iOS 8 and above. 我自定义了UITableCellDeleteConfirmationView以更改iOS 8及更高版本中按钮的背景颜色。 Following this post , I implemented editActionsForRowAtIndexPath , commitEditingStyle and canEditRowAtIndexPath . 在这篇文章之后 ,我实现了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];
}

However, when I push these custom buttons, they do not call commitEditingStyle . 但是,当我按下这些自定义按钮时,它们不会调用commitEditingStyle According to this post , commitEditingStyle is only fired when you touch the delete button. 根据这篇文章 ,只有当您点击删除按钮时,才会触发commitEditingStyle

Instead of trying to figure out how to fire commitEditingStyle , I have created methods that replicate my commitEditingStyle implementation, removeCollectedLeafLabel . 我没有尝试找出如何触发commitEditingStyle方法,而是创建了复制我的commitEditingStyle实现的方法removeCollectedLeafLabel Can a class method be an action? 类方法可以作为动作吗? How do I set the UITableViewRowAction to do something when pressed? 按下时如何设置UITableViewRowAction以执行某些操作?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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