How can I make a custom view of the 'delete-bu

2019-02-11 04:18发布

问题:

I would like to customize the delete button which is shown when performing the 'swipe to left'-action on a tableview cell. I currently set up a subclass of a UITableViewCell but also want to customize the delete-button which is being shown.

My goal is to place three buttons when swiping.

I choose for another implementation where I was using a UIScrollview in each cell.

http://www.teehanlax.com/blog/reproducing-the-ios-7-mail-apps-interface/

回答1:

This might help you.

- (void)willTransitionToState:(UITableViewCellStateMask)state
    {
        [super willTransitionToState:state];
        if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
        {
            for (UIView *subview in self.subviews)
            {
                if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"])
                {
                    UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
                    [deleteBtn setImage:[UIImage imageNamed:@"arrow_left_s11.png"]];
                    [[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
                }
            }
        }
    }

Referenced from:

Customize the delete button in UITableView

create custom delete button for uitableview

Custom Delete button On Editing in UITableView Cell



回答2:

Accepted answer will not work on iOS 7, as there is now UITableViewCellContentView in between. So subviews loop now should look like this(if you want to support older iOS versions too, use currently accepted answer for iOS 6.1-)

        for (UIView *subview in self.subviews) {
            for (UIView *subview2 in subview.subviews) {
                if ([NSStringFromClass([subview2 class]) rangeOfString:@"Delete"].location != NSNotFound) {
                    // Do whatever you want here
                }
            }
        }


回答3:

-(void)willTransitionToState:(UITableViewCellStateMask)state{
    NSLog(@"EventTableCell willTransitionToState");
    [super willTransitionToState:state];
    if((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
    {
        UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake( 320,0, 228, 66)];
        [deleteBtn setImage:[UIImage imageNamed:@"BtnDeleteRow.png"]];
        [[self.subviews objectAtIndex:0] addSubview:deleteBtn];
        [self recurseAndReplaceSubViewIfDeleteConfirmationControl:self.subviews];
        [self performSelector:@selector(recurseAndReplaceSubViewIfDeleteConfirmationControl:) withObject:self.subviews afterDelay:0];
    }
}


回答4:

The solutions above didn't work for me for iOS 7, at - (void)willTransitionToState:, the delete button wasn't in the view heirarchy so I wasn't able to manipulate anything. I ended up doing everything on - (void)didTransitionToState:. The example below was specifically for when my cells had some spacing at the top so I'm altering the frame of the delete button. If you want to customize the delete button, you can just add a view on top of the delete button or replace it with your own UIButton

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //your own stuff
        //for some reason, editingAccessoryView cannot be nil and must have a non-CGRectZero frame
        self.editingAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
    }
    return self;
}

- (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 += defined_padding;
            frame.size.height -= defined_padding;
            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];
        if (deleteButton) {
            return deleteButton;
        }
    }
    return nil;
}