How to customised delete button in iOS 8.3

| | August 10, 2015

- (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:@"CROSS.png"]];
                [[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
            }
        }
    }
}

I am trying to add image for delete button in tableview
but cant able to figure it out please help me.
Thanks in advance.

2 Responses to “How to customised delete button in iOS 8.3”

  1. You need to sub class the UITableViewCell and override layout subviews method

    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        UIView *deleteButton = nil;
    
    
        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
            //for os version less than iOS 8, i.e., iOS 7
            BOOL shouldStop = NO;
            for (UIView *subview in self.subviews) {
                for (UIView *subview2 in subview.subviews) {
                    if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
                        deleteButton = subview2;
                        shouldStop = YES;
                        break;
                    }
                }
                if (shouldStop)
                    break;
            }
    
        } else {
            //for iOS 8
            for (UIView *subview in self.subviews) {
                if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
                    deleteButton = subview;
                    break;
                }
            }
    
        }
    
        if (deleteButton) {
            UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
            [deleteBtn setImage:[UIImage imageNamed:@"cross.png"]];
            [[deleteButton.subviews objectAtIndex:0] addSubview:deleteBtn];
        }
    
    
    
    }
    
  2. Use this block and write aMethod body in self.view
    And write this method into CellForRowAtIndexPath

    - (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString* cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
      UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
      button.frame = CGRectMake(0, 0, 64, 33);
      [button setImage:[UIImage imageNamed:@"CROSS.png"]];
      [cell.view addSubview:button];
    
     // Write your own code here
    
      return cell;
    }
    

Leave a Reply