自定义 UITableViewRowAction 按钮

Custom UITableViewRowAction button

我想为 uitableviewrowaction 按钮设置顶部和底部约束

这是我的代码

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete"  handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){

       }];
        deleteAction.backgroundColor = [UIColor redColor];

       return @[deleteAction];
  }

像这样我添加了删除按钮。在 tableviewCell 中,我添加了一个 UIView 它具有顶部和底部约束。我希望删除按钮与我在 UITableviewCell 中的视图相匹配。

您可以在自定义 uitableviewcell 中设置 删除按钮 框架 class

像这样

 -(void)didTransitionToState:(UITableViewCellStateMask)state
{
    [super didTransitionToState:state];
    if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
    {

        UIView *deleteButton = [self deleteButtonSubview:self];
        if (deleteButton)
        {
            CGRect frame = deleteButton.frame;
            frame.origin.y = 4;
            frame.size.height = frame.size.height-8;
            /*
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            {

                frame.size.height = 62; //vikram singh 2/1/2015
                frame.size.width = 80;

            }
            else
            {
                frame.size.height = 52; //vikram singh 2/1/2015
                frame.size.width = 80;

            }
             */
            deleteButton.frame = frame;

        }
    }
}

- (UIView *)deleteButtonSubview:(UIView *)view
{
    if ([NSStringFromClass([view class]) rangeOfString:@"Delete"].location != NSNotFound) {
        return view;
    }
    for (UIView *subview in view.subviews) {
        UIView *deleteButton = [self deleteButtonSubview:subview];
        [deleteButton setBackgroundColor:[UIColor whiteColor]];
        if (deleteButton) {

            return deleteButton;
        }
    }
    return nil;
}

使用didTransitionToState 方法:)

关于 Balkaran 的回答的更多信息...删除按钮是自定义私有 class,但使用 String(describing:) 方法,您可以合理地确定您可以获取它。
此外,我惊讶地发现 didTransition 在您开始更改状态时立即触发,而不是在状态完成更改时触发。

Swift 3 中@balkaran 代码的更新版本:

override func didTransition(to state: UITableViewCellStateMask) {
    super.willTransition(to: state)
    if state == .showingDeleteConfirmationMask {
        let deleteButton: UIView? = subviews.first(where: { (aView) -> Bool in
            return String(describing: aView).contains("Delete")

        })
        if deleteButton != nil {
            deleteButton?.frame.size.height = 50.0
        }
    super.willTransitionToState(state)
    if state == .ShowingDeleteConfirmationMask
    {
       let deleteButton: UIView? = subviews[0]

        if deleteButton != nil {
            var frame: CGRect? = deleteButton?.frame
            frame?.origin.y = 9
            frame?.origin.x = 10
            frame?.size.height = (frame?.size.height)! - 14
            deleteButton?.frame = frame!
        }
    }