[英]How to set the action of UITableViewRowAction
我自定义了UITableCellDeleteConfirmationView以更改iOS 8及更高版本中按钮的背景颜色。 在这篇文章之后 ,我实现了editActionsForRowAtIndexPath
, commitEditingStyle
和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];
}
但是,当我按下这些自定义按钮时,它们不会调用commitEditingStyle
。 根据这篇文章 ,只有当您点击删除按钮时,才会触发commitEditingStyle
。
我没有尝试找出如何触发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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.