How can I make a custom view of the 'delete-button' when performing commitEditingStyle within a UITableView

后端 未结 4 883
囚心锁ツ
囚心锁ツ 2021-01-03 13:20

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 UITableViewCe

4条回答
  •  执笔经年
    2021-01-03 14:13

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

提交回复
热议问题