How add custom image to uitableview cell swipe to

2020-02-25 23:48发布

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

2条回答
看我几分像从前
2楼-- · 2020-02-25 23:53

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")!)
查看更多
爷、活的狠高调
3楼-- · 2020-02-25 23:57

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

查看更多
登录 后发表回答