LampPost
LampPost

Reputation: 866

How to show image when triggering UITableViewRowAction?

I'm trying to customize the where the delete option has a red background and a trash icon. So far from what I gathered and implemented I have a repeating image in the background and not sure how to make it red or show only 1 trash can image

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let title = "          "
        let deleteAction = UITableViewRowAction(style: .default, title: title) { (action, indexpath) in
        }
        let image = UIImage(named: "icon_trash")
        if let im = image {
            deleteAction.backgroundColor = UIColor(patternImage: im)
        }
        return [deleteAction]
    }

image

I can see I'm close but not sure how to piece the color and what I have together to show something like the image below

image2

Upvotes: 1

Views: 389

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100523

I think it's time to upgrade to these ios 11 new methods with image property

public func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
public func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?

So try this

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let deleteAction = UIContextualAction(style: .normal, title:  "", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        // Call edit action

        // Reset state
        success(true)
    })
    deleteAction.image = UIImage(named: "icon_trash")
    deleteAction.backgroundColor = .red
    return UISwipeActionsConfiguration(actions: [deleteAction])
}

Upvotes: 3

Related Questions