Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 384
Palash Sharma Avatar asked Dec 29 '25 15:12

Palash Sharma


2 Answers

I don't think that you can insert anything into this default delete button, and you will need to build your own one. There is a nice guide on Ray Wenderlich's site that should help you to achieve this: check this link...

like image 52
JBA Avatar answered Jan 01 '26 07:01

JBA


In Swift

class TableViewRowAction: UITableViewRowAction 
{
    var image: UIImage?

    func _setButton(button: UIButton)  {

        if let image = image, let titleLabel = button.titleLabel {

             let labelString = NSString(string: titleLabel.text!)
             let titleSize = labelString.sizeWithAttributes([NSFontAttributeName: titleLabel.font])
             button.tintColor = UIColor.whiteColor()
             button.setImage(image.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
             button.imageEdgeInsets.right = -titleSize.width
        }
    }
}


func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {


    let moreRowAction = TableViewRowAction(style: UITableViewRowActionStyle.Default, title: "        ") { action, indexPath in
        // Action Block of more button.
    }

    moreRowAction.image = UIImage(named: "edit_white")
    moreRowAction.backgroundColor = UIColor(red: 252/255, green: 65/255, blue: 75/255, alpha: 1.0)



    let deleteRowAction = TableViewRowAction(style: UITableViewRowActionStyle.Default, title: "        ") { action, indexPath in
          // Action Block of delete button.

    }

    deleteRowAction.backgroundColor = UIColor(red: 252/255, green: 65/255, blue: 75/255, alpha: 1.0)
    deleteRowAction.image = UIImage(named: "delete_white")

    return [deleteRowAction, moreRowAction]

}
like image 26
Anit Kumar Avatar answered Jan 01 '26 05:01

Anit Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!