13

I have an UITableView and I've added edit actions to it. Now I want to add image above the delete buttons label, as:

enter image description here

This is my code:

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

    let blockAction = UITableViewRowAction(style: .Normal, title: "Block") { (rowAction:UITableViewRowAction, indexPath:NSIndexPath) -> Void in
        //TODO: Delete the row at indexPath here
    }
    blockAction.backgroundColor = UIColor.redColor()

    return [blockAction]
}

How can I add image on my Delete button?

Orkhan Alizade
  • 7,379
  • 14
  • 40
  • 79
  • @rmaddy but how other developers do that? – Orkhan Alizade Nov 26 '15 at 15:33
  • Perhaps they are using a 3rd party custom table view cell class and not using the standard API provided by the `UITableViewDelegate`. – rmaddy Nov 26 '15 at 15:35
  • 1
    refer to this questions, you will find your answer: http://stackoverflow.com/questions/20290766/change-the-color-of-default-red-color-delete-button-in-uitableviewcell-when-swip ---------------- http://stackoverflow.com/questions/29335104/how-add-custom-image-to-uitableview-cell-swipe-to-delete if you are on iOS8+ – sbsr Nov 26 '15 at 15:48
  • Only the 2nd link is relevant. I forgot about the idea of using a "pattern image" for the background color. That could work. – rmaddy Nov 26 '15 at 16:19

2 Answers2

2

You can use special image on background color and use '\n' symbol for let down text

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let blockAction = UITableViewRowAction(style: .normal, title: "\nBlock") { (rowAction:UITableViewRowAction, indexPath:IndexPath) -> Void in
        //TODO: Delete the row at indexPath here
    }
    blockAction.backgroundColor = UIColor(patternImage: UIImage(named: "delete_background")!)

    return [blockAction]
}
0

You can implement a swipeable tableviewcell to accomplish this where you can implement custom button for edit actions. There are lot of tutorials on this. Check the below one:

https://www.raywenderlich.com/62435/make-swipeable-table-view-cell-actions-without-going-nuts-scroll-views

Arun Gupta
  • 2,628
  • 1
  • 20
  • 37