【问题标题】:tableview multiple selection using edit mode使用编辑模式的tableview多选
【发布时间】:2020-11-02 14:01:14
【问题描述】:

您好,我正在创建 swift 应用程序,我正在使用最近的聊天列表创建 tableview,并且在 tableview 中我需要选择具有编辑模式的多行这里是我的屏幕截图我真正想要的和我尝试过的代码

这是启用编辑模式时的屏幕截图

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

选中行后的屏幕截图

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

这是我点击编辑按钮时的代码

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

但问题是在启用编辑模式后如何显示屏幕截图中选择的行可以请任何人帮助我,我参考了一些答案如下

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

【问题讨论】:

  • 谁能帮我解决这个问题

标签: ios swift tableview multipleselection


【解决方案1】:

请检查一下,可能会对您有所帮助。长按或点击编辑按钮即可编辑。

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!
        
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多