Dave
Dave

Reputation: 12216

Add Image To Button In UITableView's "editActionsForRowAt"

A while ago I asked a question on how to add more than the standard editActionsForRowAt "delete" button. Thanks to the posted answers, my app now has the 3-button swipe-to-edit functionality like below!

enter image description here

However, with iOS9 came pretty little icons to accompany the text... and now I have a new question, how do I get that functionality?

enter image description here

I've Googled many searches like "Add image to editActionsForRowAt" and found nothing helpful. If you have code, a tutorial link, or anything that could get me headed in the right direction, it would be greatly appreciated.

Upvotes: 1

Views: 1558

Answers (1)

Dave
Dave

Reputation: 12216

One of the comments lead me to using unicode characters! Super helpful as they can be added to your normal string. Unicode characters include things like:

Useful unicode character examples Useful unicode character examples 2 Useful unicode character examples 3

So all you do is pass a string with a unicode character, then a line break (\n), then your button title/text. So the basic syntax looks like:

title: "\u{1234}\n YourText "

For easy reuse (because I have multiple tables) I put the characters I wanted into a struct:

struct cellEditActionIcons {
    static var editAction_Arrow = "\u{2191}"
    static var editAction_Check = "\u{2713}"
    static var editAction_Delete = "\u{2715}"
} 

Then you can create the buttons like this:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    var doneBut = UITableViewRowAction (style: .normal, title: "\(cellEditActionIcons.editAction_Check)\n YourText" { action, index in
        //Do stuff
    }
    doneBut.backgroundColor = myColors.someColor
}

Upvotes: 3

Related Questions