简体   繁体   中英

Cell swipe change delete text to an icon in iOS

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM