Custom UITableViewRowAction button

依然范特西╮ 提交于 2019-12-19 10:24:04

问题


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


来源:https://stackoverflow.com/questions/42022696/custom-uitableviewrowaction-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!