Swift中带有多选复选框的可扩展下拉菜单

Posted

技术标签:

【中文标题】Swift中带有多选复选框的可扩展下拉菜单【英文标题】:Expandable drop down with Multi select checkbox in Swift 【发布时间】:2021-02-03 00:21:45 【问题描述】:

通过点击表格视图中的单元格来选中/取消选中复选框,以及如何知道在 Swift 的 Expandable 下拉菜单中哪个单元格已选中或未选中。

VBExpandVC

class VBExpandVC: UIViewController,UITableViewDelegate,UITableViewDataSource 

    @IBOutlet var myTableView: UITableView!
    
    struct Notification:Codable 
        let notification:[Headings]
    
    
    struct Headings:Codable 
        var name:String
        var status:Int
    
       
    var names = [Headings]()

    var expandTableview:VBHeader = VBHeader()
    var cell : VCExpandCell!
    
    override func viewDidLoad() 
        super.viewDidLoad()
        
        getNotifications()
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
    
        expandTableview = Bundle.main.loadNibNamed("VBHeader", owner: self, options: nil)?[0] as! VBHeader
        let layer = expandTableview.viewHeader.layer
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOffset = CGSize(width: 0, height: 1)
        layer.shadowOpacity = 0.4
        expandTableview.lblDate.text = self.names[section].name
        expandTableview.btnExpand.tag = section
        expandTableview.btnExpand.addTarget(self, action: #selector(VBExpandVC.headerCellButtonTapped(_sender:)), for: UIControl.Event.touchUpInside)

        let str:String = "\(self.names[section].status)"//arrStatus[section] as! String
        if str == "0"
        
            UIView.animate(withDuration: 2)  () -> Void in
                self.expandTableview.imgArrow.image = UIImage(named :"switch")
            
        
        else
        
            UIView.animate(withDuration: 2)  () -> Void in
                self.expandTableview.imgArrow.image = UIImage(named :"switch2")
            
        

        return expandTableview
    

    @objc func headerCellButtonTapped(_sender: UIButton)
    
        print("header tapped at:")
        print(_sender.tag)
        var str:String = "\(self.names[_sender.tag].status)"
        if str == "0"
        
            self.names[_sender.tag].status = 1
        
        else
        
            self.names[_sender.tag].status = 0
        
//        myTableView.reloadData()
        myTableView.reloadSections([_sender.tag], with: .none)
    

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
    
        //Return header height as per your header hieght of xib
        return 40
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
        let str:Int = (names[section].status)
        if str == 0
        
            return 0
        
        return  1
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
        cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? VCExpandCell
        return cell;
    

    func numberOfSections(in tableView: UITableView) -> Int
    
        return self.names.count
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    
        //Return row height as per your cell in tableview
        return 111
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
        print("selected:\(indexPath.section)")
    
    
    // getNotifications
    func getNotifications()
        guard let url = URL(string: "https://www.json-generator.com/api/json/get/cgAhRPmZgy?indent=2") else 
            return
        
        var request = URLRequest(url: url)
        request.httpMethod = "GET"
        
        URLSession.shared.dataTask(with: request, completionHandler:  (data, response, error) in
            guard let data = data, error == nil, response != nil else 
                return
            
            
            do 
                let headings = try JSONDecoder().decode(Notification.self, from: data)
                self.names = headings.notification
                DispatchQueue.main.async 
                    self.myTableView.reloadData()
                
             catch 
                print(error)
            
        ).resume()
    
    // End

VCExpandCell

class VCExpandCell: UITableViewCell 
    
    @IBOutlet weak var btnMobile: UIButton!
    @IBOutlet weak var btnEmail: UIButton!
    @IBOutlet weak var btnSms: UIButton!
    
    override func awakeFromNib() 
        super.awakeFromNib()
        // Initialization code
    

    override func setSelected(_ selected: Bool, animated: Bool) 
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    
    
    @IBAction func btnMobileApp(_ sender: UIButton) 
        print("mobile app checked")
        print(sender.tag)
        if sender.isSelected 
            sender.isSelected = false
         else 
            sender.isSelected = true
        
    
    
    @IBAction func btnSMS(_ sender: UIButton) 
        print("sms checked")
        print(sender.tag)

        if sender.isSelected 
          sender.isSelected = false
       else 
          sender.isSelected = true
      

    

    @IBAction func btnEmail(_ sender: UIButton) 
        print("email checked")
        print(sender.tag)
        if sender.isSelected 
            sender.isSelected = false
         else 
            sender.isSelected = true
        
    
    

enter image description here

在上面的代码中,我有两个主要问题。

    当展开该部分并展开另一个部分时,选定的复选框位置会发生变化

    无法通过点击展开下拉列表中表格视图中的单元格来找到选中的复选框。

【问题讨论】:

【参考方案1】:

看看他给定的网址:

https://github.com/AssistoLab/DropDown

【讨论】:

以上是关于Swift中带有多选复选框的可扩展下拉菜单的主要内容,如果未能解决你的问题,请参考以下文章

swift 3中的多选下拉菜单

下拉框多选框单选框 通过TagHelper绑定数据

带有选择所有复选框的多选下拉菜单

如何在 Asp.NET MVC 中使用复选框创建多选下拉菜单

使用带复选框的多选下拉菜单搜索或过滤 jquery 数据表中的列

在 s-s-rS 报告中使用级联多选下拉菜单时,取消选中所有复选框不起作用