1

Hello i am creating swift app and i am creating tableview with recent chat list ans in tableview i need to select multiple row with edit mode here is my screen shot what i exactly want and my code which i have tried

Here is screen shot while edit mode is enabled

https://i.stack.imgur.com/2W21M.jpg

and below screen shot when selected rows

https://i.stack.imgur.com/XDgfe.png

and here is my code when i click on edit button

self.tblListView.allowsMultipleSelectionDuringEditing = true
self.tblListView.setEditing(true, animated: true)

but issue is that after enable edit mode how to show rows selected as in screen shot can any one please help me i have refer some answer as below

I have check this ans but it won't work for me

gipeje
  • 61
  • 7

1 Answers1

0

plz check this, may be help you. long press or by click on edit button you can edit.

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!
        
    }
Jins George
  • 121
  • 6