[英]How to remove a cell from a TableView from another ViewController when tap on UIButton (Swift)
[英]Swift - remove cell from TableView after Button-Tap
在我的项目中,我有一个UITableView
。 其中的cells
包含一个UIButton
,其作用类似于“复选框”,因此用户可以勾选任务。
我的问题:如何在用户按下其中的UIButton
后删除cell
?
这是我的customCell
:
import UIKit
class WhishCell: UITableViewCell {
let label: UILabel = {
let v = UILabel()
v.font = UIFont(name: "AvenirNext", size: 23)
v.textColor = .white
v.font = v.font.withSize(23)
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let checkButton: UIButton = {
let v = UIButton()
v.backgroundColor = .darkGray
// v.layer.borderColor = UIColor.red.cgColor
// v.layer.borderWidth = 2.0
v.translatesAutoresizingMaskIntoConstraints = false
v.setBackgroundImage(UIImage(named: "boxUnchecked"), for: .normal)
return v
}()
public static let reuseID = "WhishCell"
required init?(coder: NSCoder) {fatalError("init(coder:) has not been implemented")}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.backgroundColor = .clear
// add checkButton
self.contentView.addSubview(checkButton)
self.checkButton.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
self.checkButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
self.checkButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
self.checkButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
self.checkButton.addTarget(self, action: #selector(checkButtonTapped), for: .touchUpInside)
// add label
self.contentView.addSubview(label)
self.label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 70).isActive = true
self.label.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
}
@objc func checkButtonTapped(){
self.checkButton.setBackgroundImage(UIImage(named: "boxChecked"), for: .normal)
self.checkButton.alpha = 0
self.checkButton.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
UIView.animate(withDuration: 0.3) {
self.checkButton.alpha = 1
self.checkButton.transform = CGAffineTransform.identity
}
}
}
当您创建单元格并设置按钮选择器时,您只需要像这样设置按钮标签和选择器:
self.checkButton.tag = indexPath.row
self.checkButton.addTarget(self, action: #selector(checkButtonTapped(sender:)), for: .touchUpInside)
并在选择器中进行更改:
@objc func checkButtonTapped(sender : UIButton){
let index = sender.tag
self.tableView.deleteRows(at: [index], with: .automatic)
//Add all your code here..
}
谢谢
您可以使用此扩展名访问单元格中的tableView
和indexPath
:
extension UITableViewCell {
var tableView: UITableView? {
return (next as? UITableView) ?? (parentViewController as? UITableViewController)?.tableView
}
var indexPath: IndexPath? {
return tableView?.indexPath(for: self)
}
}
在这个其他扩展的帮助下:
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}
那么你可以像往常一样删除单元格:
tableView!.deleteRows(at: [indexPath!], with: .automatic)
请注意, tableView应该负责管理单元格。
一个快速的方法是回调闭包。
在单元格中添加一个属性
var callback : ((UITableViewCell) -> Void)?
在动作中调用回调并传递单元格
@objc func checkButtonTapped(){
...
callback?(self)
}
在cellForRowAt
的控制器中为回调属性分配一个闭包
cell.callback = { cell in
let indexPath = tableView.indexPath(for: cell)!
// insert here a line to remove the item from the data source array.
tableView.deleteRows(at: [indexPath], with: .automatic)
}
这种原生 Swift 解决方案比分配标签、 objective-c-ish目标/动作或繁琐的视图层次结构数学要高效得多。
改变你的 checkButtonTapped 这个
@objc func checkButtonTapped() {
self.checkButton.setBackgroundImage(UIImage(named: "boxChecked"), for: .normal)
self.checkButton.alpha = 0
self.checkButton.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
UIView.animate(withDuration: 0.3) {
self.checkButton.alpha = 1
self.checkButton.transform = CGAffineTransform.identity
}
if let tableView = self.superview as? UITableView {
let indexPath = tableView.indexPath(for: self)
tableView.dataSource?.tableView!(tableView, commit: .delete, forRowAt: indexPath!)
}
}
并将下一个方法添加到您的 UITableViewDelegate 实现中
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("Deleted")
self.texts.remove(at: indexPath.row)
self.tableview.deleteRows(at: [indexPath], with: .automatic)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.