пожалуйста проверьте это, может быть вам поможет. длительное нажатие или щелчок по кнопке редактирования вы можете редактировать.
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate{
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var btnDone: UIButton!
var nameArray = [String]()
var numberArray = [String]()
var datas = [[String : Any]]()
var press = ""
override func viewDidLoad() {
super.viewDidLoad()
nameArray = ["Test Name 1","Test Name 2","Test Name 3","Test Name 4","Test Name 5","Test Name 6","Test Name 7","Test Name 8","Test Name 9","Test Name 10"]
numberArray = ["9520157410","9500187411","9420057812","9520157413","9520157414","9520157415","9520157416","9520157417","9520157418","9520157419"]
for i in 0..<nameArray.count
{
datas.append(["name":nameArray[i],"number":numberArray[i], "status":"0"])
}
btnDone.isHidden = true
setupLongPressGesture()
}
func setupLongPressGesture() {
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tableView.addGestureRecognizer(longPressGesture)
}
@objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer){
if gestureRecognizer.state == .began {
press = "long"
self.tableView.reloadData()
btnDone.isHidden = false
let touchPoint = gestureRecognizer.location(in: self.tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
}
}
}
@IBAction func btnedit(_ sender: Any) {
press = "long"
self.tableView.reloadData()
btnDone.isHidden = false
}
@IBAction func btnDone(_ sender: Any) {
press = ""
btnDone.isHidden = true
datas.removeAll()
for i in 0..<nameArray.count
{
datas.append(["name":nameArray[i],"number":numberArray[i], "status":"0"])
}
tableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datas.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! cell
let val = datas[indexPath.row]
cell.descriptionName.text = (val["name"] as! String)
cell.valueNumber.text = (val["number"] as! String)
if press == "long"{
cell.width.constant = 35
if (val["status"] as! String) == "1"{
cell.btnSelect.setImage(UIImage(systemName: "circle.fill"), for: .normal)
}else{
cell.btnSelect.setImage(UIImage(systemName: "circle"), for: .normal)
}
cell.btnSelect.isHidden = false
cell.btnSelect.addTarget(self, action: #selector(subscribeTapped(_:)), for: .touchUpInside)
}else{
cell.width.constant = 0
cell.btnSelect.isHidden = true//circle.fill
}
cell.btnSelect.tag = indexPath.row
return cell
}
@objc func subscribeTapped(_ sender: UIButton){
var sel = datas[sender.tag]
datas.remove(at: sender.tag)
if sel["status"] as! String == "1"{
sel.updateValue("0", forKey: "status")
}else{
sel.updateValue("1", forKey: "status")
}
datas.insert(sel, at: sender.tag)
tableView.reloadData()
}
}
class cell: UITableViewCell {
@IBOutlet weak var imgProfile: UIImageView!
@IBOutlet weak var width: NSLayoutConstraint!
@IBOutlet weak var btnSelect: UIButton!
@IBOutlet weak var descriptionName: UILabel!
@IBOutlet weak var valueNumber: UILabel!
}