Custom UITableViewRowAction button

2020-03-27 11:19发布

问题:

I want to set top and bottom constraint for uitableviewrowaction button

Here's my code

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

Like this I've added delete button. In tableviewCell I've added one UIView it has top and bottom constraints. I want the delete button to match with my view in UITableviewCell.

回答1:

you can set delete button frame in your custom uitableviewcell class

like this

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

use didTransitionToState methods :)



回答2:

A little more about Balkaran's answer... The delete button is a custom private class, but using the String(describing:) method, you can be reasonably sure you can get ahold of it.
Also, I was surprised to find that didTransition fires as soon as you start changing the state, not when the state is done changing.

An updated version of @balkaran's code in Swift 3:

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
        }


回答3:

    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!
        }
    }