How add custom image to uitableview cell swipe to delete

后端 未结 3 580
有刺的猬
有刺的猬 2020-12-30 09:03

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

3条回答
  •  难免孤独
    2020-12-30 09:29

    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

提交回复
热议问题