3

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

Palash Sharma
  • 662
  • 1
  • 10
  • 18

4 Answers4

2

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

JBA
  • 2,889
  • 1
  • 21
  • 38
2

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]

}
Anit Kumar
  • 8,075
  • 1
  • 24
  • 27
1

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

https://cocoapods.org/pods/SWTableViewCell

arango_86
  • 4,236
  • 4
  • 40
  • 46
0

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];
        }
    }
}
}
Community
  • 1
  • 1
Ashraf Tawfeeq
  • 3,036
  • 1
  • 20
  • 34