微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

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. 选中的复选框位置在展开部分和展开另一部分时发生变化

  2. 无法通过在“展开”下拉菜单中点击表格视图中的单元格来找到选定的复选框。

解决方法

看看他给定的网址:

https://github.com/AssistoLab/DropDown

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。