iOS tableView左滑刪除按鈕的自定義
本文僅做日常開發記錄
遇到一個需求如下圖:
左滑刪除的時候,要刪除按鈕也有圓角
實現方式就是拿到刪除的那個視圖,然後修改layer,代碼如下:
如有小夥伴也需要,僅供參考,因爲這樣寫,是不安全的,直接上代碼:
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TestCell
cell.backgroundColor = UIColor.white
if indexPath.row % 2 == 0 {
cell.contentView.backgroundColor = .systemYellow
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
90
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
guard editingStyle == .delete else {
return
}
//點擊刪除會觸發
print("____shan chu cell")
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
return "刪除"
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//guard let cell = self.feelCell else { return }
//cell.topLayer?.frame = cell.bgImageView.bounds
cell.contentView.layer.cornerRadius = 15
cell.contentView.layer.masksToBounds = true
let img = UIImageView()
img.contentMode = .scaleAspectFill
}
@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = UIContextualAction(style: UIContextualAction.Style.normal, title: "刪除") { [weak self] _, _, complete in
print("___刪除了 cell")
complete(true)
}
//delete.image = UIImage(named: "aaa")
//delete.imageView!.contentMode = .scaleAspectFill
//delete.image.contentMode = .scaleAspectFill
delete.backgroundColor = UIColor.systemGreen
let action = UISwipeActionsConfiguration(actions: [delete])
action.performsFirstActionWithFullSwipe = false
return action
}
//滑動刪除的時候會調用
func tableView(_ tab: UITableView, willBeginEditingRowAt: IndexPath) {
print("____開始刪除")
test()
}
爲刪除按鈕添加圓角
extension ViewController {
//iOS 11 和iOS 12獲取刪除按鈕的方法
private func test() {
for subView in tableView.subviews {
print("___sub:___\(subView)____\(subView.classForCoder)")
//_UITableViewCellSwipeContainerView
if NSStringFromClass(subView.classForCoder) == "_UITableViewCellSwipeContainerView"{
for sub in subView.subviews {
if NSStringFromClass(sub.classForCoder) == "UISwipeActionPullView" {
sub.backgroundColor = .clear
if let delBtn: UIButton = sub.subviews.first as? UIButton {
//此處可以拿到刪除按鈕後做一些相應的操作
print(">>>完全自定義___\(delBtn)")
delBtn.isHidden = false
delBtn.layer.cornerRadius = 9.0
delBtn.clipsToBounds = true
}
sub.backgroundColor = .systemRed
sub.layer.cornerRadius = 9.0
sub.layer.masksToBounds = true
}
}
}
}
}
}
實現效果如下:
有需要的小夥伴,可以自取,親測沒問題
ps:
iOS9、iOS11、iOS13具體獲取到對應刪除按鈕的方式不同
iOS9、iOS11添加按鈕的方式有所不同
自定義的的控件的寬度存在一些差異