1

I'm using Xcode 9 version and right now I'm implementing a tableview with cell. When the cell is swiped, the delete button must be shown. It’s working now. But I want to change the delete from text to an icon. What I've done so far is:

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

But the above code is not working. Maybe that is working in previous Xcode version but not in the latest. Do you have any idea on how to fix this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
qazzu
  • 361
  • 3
  • 5
  • 16
  • There is no public API to change the text to an icon. There is a delegate method to change the text. Any attempt to dig into the private subview structure is just an exercise in futility. That structure can change at any iOS update (this has nothing to do with Xcode). – rmaddy Nov 01 '17 at 02:45
  • oh my lordt, stay away from digging into and looking/editting subviews like this. A lot of these are considered private and this can very easily cause issues from one iOS version the next – gadu Nov 01 '17 at 02:46
  • @rmaddy I see. Is the delegate method can also change the text to an icon? – qazzu Nov 01 '17 at 02:52
  • @qazzu As I stated in my previous comment, no. – rmaddy Nov 01 '17 at 02:53
  • @rmaddy If that is the case. Do you have suggestions on how can I achieve it? – qazzu Nov 01 '17 at 02:55

1 Answers1

0

I don't know if this will work, but building off this post you can try something like:

 -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
 UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
    {
        NSLog(@"Action to perform with Button 1");
    }];
    button.backgroundColor = [UIColor redColor]; // red to keep it the same, or whatever you want
    [button setImage:[UIImage imageNamed:@"delete_icon.png"]];

    return @[button];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// you need to implement this method too or nothing will work:

}
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES;
    }
gadu
  • 1,816
  • 1
  • 17
  • 31