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
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...
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]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With