I want to delete a tableview cell row when the button deleteButton is triggered. You can see exactly what I am looking for in the gif below. When the user taps the deleteButton is should call the func didDelete. Right now if you press the button nothing happens. And every time the delete button is called it should delete the cell which the button was in.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private var myArray: [String] = ["First"]
var myTableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
myTableView.dataSource = self
myTableView.delegate = self
self.view.addSubview(myTableView)
myTableView.translatesAutoresizingMaskIntoConstraints = false
myTableView.register(MyCell.self, forCellReuseIdentifier: "MyCell")
NSLayoutConstraint.activate([
myTableView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.90),
myTableView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1),
myTableView.topAnchor.constraint(equalTo: view.topAnchor),
myTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
])
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Num: \(indexPath.row)")
print("Value: \(myArray[indexPath.row])")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
cell.label.text = ""
cell.buttonTapCallback = { [weak self] in
guard let self = self else { return }
self.myArray.append("NewItem")
self.myTableView.reloadData()
}
cell.didDelete = { [weak self] in
guard let self = self else { return }
self.myArray.remove(at:)
self.myTableView.reloadData()
}
return cell
}
}
class MyCell: UITableViewCell {
var buttonTapCallback: () -> () = { }
var didDelete: () -> () = { }
let button: UIButton = {
let btn = UIButton()
btn.setTitle("Button", for: .normal)
btn.backgroundColor = .systemPink
btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
return btn
}()
let deleteButton: UIButton = {
let btn = UIButton()
btn.setTitle("Delete", for: .normal)
btn.backgroundColor = .green
btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
return btn
}()
let label: UILabel = {
let lbl = UILabel()
lbl.font = UIFont.systemFont(ofSize: 16)
lbl.textColor = .systemPink
return lbl
}()
@objc func didTapButton() {
buttonTapCallback()
}
@objc func didTapDelete() {
didDelete()
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//Add button
contentView.addSubview(button)
contentView.addSubview(deleteButton)
button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
deleteButton.addTarget(self, action: #selector(didDelete), for: .touchUpInside)
//Set constraints as per your requirements
button.translatesAutoresizingMaskIntoConstraints = false
button.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20).isActive = true
button.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10).isActive = true
button.widthAnchor.constraint(equalToConstant: 100).isActive = true
button.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -10).isActive = true
contentView.addSubview(deleteButton)
//Set constraints as per your requirements
deleteButton.translatesAutoresizingMaskIntoConstraints = false
deleteButton.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: 20).isActive = true
deleteButton.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10).isActive = true
deleteButton.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -10).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}