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

后端 未结 4 878
囚心锁ツ
囚心锁ツ 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 13:56

    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

    0 讨论(0)
  • 2021-01-03 14:05

    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
                    }
                }
            }
    
    0 讨论(0)
  • 2021-01-03 14:09
    -(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];
        }
    }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题