3

I've already searched this question but what I found was:

  1. How to put the image as the row action background.

  2. How to put an unicode character \u{unicode here}. It worked, but I couldn't find an unicode character to represent an add button, or a delete button perhaps.

But what I want is to put a custom image on top of the row action title. For example: a trash can with the title delete below it.

I've tried this code too:

"\(UIImage(named: "trashImg"))"

In the row action title string, but xcode didn't seem to count the last ) as part of the code, but only as part of the string. But anyway, I built the project and when I tested, instead of the title, there was a huge text saying some stuff like "optional: (64,)...." etc, it didn't crash tho.

Please help me, many apps do this and I want it on mine too!

Thanks in advance.

Henrique Dourado
  • 320
  • 1
  • 7
  • 16

2 Answers2

2

You need to change insets according to your requirements.

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
0

You are putting the image into a string object which will get the default string representation of that image.

Perhaps this post may help. Looks like they are overriding editActionsForRowAtIndexPath and setting the background color with a patternimage. Custom table view row action (image)

Another post saying basically the same thing UITableViewRowAction image for title

Community
  • 1
  • 1
hawkstrider
  • 4,141
  • 16
  • 27
  • I've already tried that, but it just makes the image be the background, not it appears. – Henrique Dourado Mar 11 '16 at 18:32
  • It sounds like you may just need to create a custom UITableViewCell and place your own custom button with the image and title on the right of the cell. Is there a reason that you have to use the editAction? – hawkstrider Mar 11 '16 at 18:37