Palash Sharma
Palash Sharma

Reputation: 682

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

Upvotes: 3

Views: 5908

Answers (4)

arango_86
arango_86

Reputation: 4425

Are you using CocoaPods in your project. Check out "SWTableViewCell". It may help you to implement this functionality.

https://cocoapods.org/pods/SWTableViewCell

Upvotes: 1

Anit Kumar
Anit Kumar

Reputation: 8153

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]

}

Upvotes: 2

Ashraf Tawfeeq
Ashraf Tawfeeq

Reputation: 3029

I think this answer maybe what's you're looking for https://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];
        }
    }
}
}

Upvotes: 0

JBA
JBA

Reputation: 2909

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...

Upvotes: 2

Related Questions