8th Grader 8th Grader - 2 months ago 19
Objective-C Question

How to add image to UITableViewCell delete button

I have made an editable UITableView and Now I want to add an Image to the delete button. Till now my code is this:

Code

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"×" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
NSManagedObject *record = [self.fetchedResultsController objectAtIndexPath:indexPath];

if (record) {
[self.fetchedResultsController.managedObjectContext deleteObject:record];
}
}];
button.backgroundColor = UIColorFromRGB(0x0d9de5); //arbitrary color

return @[button];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

}


Please Help Me!

Cheers,

Palash

Answer

I think this answer maybe what's you're looking for http://stackoverflow.com/a/12511432/3342901

Quoting:

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