Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How add custom image to uitableview cell swipe to delete

Could you tell me, how to add custom image to delete button when swipe cell on UITableview?

like image 322
user3742622 Avatar asked Mar 29 '15 21:03

user3742622


People also ask

How do you customize swipe edit buttons in Uitableview?

As of iOS 8.0 there's an easy way to customize the list of buttons that appear when the user swipes from right to left: editActionsForRowAt . Return an array of UITableViewRowAction objects that have titles and styles (and also background colors if you want to customize their appearance), and iOS does the rest.

How do I swipe to delete Uitableviewcells?

When you want to handle deleting, you have to do three things: first, check that it's a delete that's happening and not an insert (this is down to how you use the UI); second, delete the item from the data source you used to build the table; and third, call deleteRows(at:) on your table view.

What is Uitableview in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


2 Answers

search you need function "editActionsForRowAtIndexPath", where you create scope of actions. You need to set UIImage to backgroundColor of UITableViewRowAction.

let someAction = UITableViewRowAction(style: .Default, title: "") { value in 
    println("button did tapped!")
}
someAction.backgroundColor = UIColor(patternImage: UIImage(named: "myImage")!)
like image 91
dimpiax Avatar answered Sep 20 '22 16:09

dimpiax


There's this UITableView delegate function you can make use of:

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let deleteAction = UIContextualAction(style: .normal, title: "", handler: {a,b,c in
        // example of your delete function
        self.YourArray.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .automatic)
    })

    deleteAction.image = UIImage(named: "trash.png")
    deleteAction.backgroundColor = .red
    return UISwipeActionsConfiguration(actions: [deleteAction])
}

PS: Personally, I think icon size 32 is the best

like image 32
ninahadi Avatar answered Sep 19 '22 16:09

ninahadi