简体   繁体   中英

How can I keep the tableview sections in a condition?

I have two sections and these cells have checkboxes. These checkbox control tableview expand movement. In my code working well for section zero when I click checkbox of section zero is expanded but when I try to make it same thing for section one, I have error because I can't make section detection.When I click section one checkbox its continue close section zero. How can I reach my tableview sections in my action function?

Here my checkbox action function code:

    @objc func onValueChanged(sender : M13Checkbox?){

    switch sender!.checkState {
        case .checked:
                // sender a int vererek bir dene bakalım belki çalışır

                let indexPath = IndexPath(row: (sender!.tag), section: 0)


               let indexPathTeslim = IndexPath(row: (sender!.tag), section: 1)

                let adresso = self.userAdressDict[indexPath.row]
                let adressoTeslim = self.userAdressDictForTeslim[indexPathTeslim.row]
                //print("\(adressoTeslim.userIlce!)" + " / " + "\(adressoTeslim.userMahalle!)" + " / " + "\(adressoTeslim.userSokak!)" + " / " + "\(adressoTeslim.userApartman!)" + " / No: " + "\(adressoTeslim.userNumara!)" + " / Kat: " + "\(adressoTeslim.userKat!)")

                //print("bu alım adresidir: ")
                //print("\(adresso.userIlce!)" + " / " + "\(adresso.userMahalle!)" + " / " + "\(adresso.userSokak!)" + " / " + "\(adresso.userApartman!)" + " / No: " + "\(adresso.userNumara!)" + " / Kat: " + "\(adresso.userKat!)")

                let adresAlim = ("\(adresso.userMahalle!)" + " / " + "\(adresso.userSokak!)" + " / " + "\(adresso.userApartman!)")
                secilenAdresForAlim = adresAlim

                let deadlineTime = DispatchTime.now() + .milliseconds(500)
                DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
                    if indexPath.section == 0 {
                        if self.keys[0] == 0 {
                            self.keys[0] = 1
                        }else{
                            self.keys[0] = 0
                            self.buttonLabel = "Değiştir"
                            self.headerTitleForSecZero = self.secilenAdresForAlim
                        }
                    }

                    DispatchQueue.main.async {
                        self.tableView.reloadData()
                    }

                    if indexPathTeslim.section == 0 {
                        if self.keysTeslim[0] == 0 {
                            self.keysTeslim[0] = 1
                        }else{
                            self.keysTeslim[0] = 0

                        }
                    }


                    DispatchQueue.main.async {
                        self.tableView.reloadData()
                    }
                }


        break

        case .unchecked:
            print("adres kaldırıldı bile...")
        break

        case .mixed:
            //empty...
        break
    }


}

And here my cellForRow function code:

        if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "AdressCell", for: indexPath) as! AdressCell
        let adresGelen = self.userAdressDict[indexPath.item]
        cell.adresLabel.text = "\(adresGelen.userIlce!)" + " / " + "\(adresGelen.userMahalle!)" + " / " + "\(adresGelen.userSokak!)" + " / " + "\(adresGelen.userApartman!)" + " / No: " + "\(adresGelen.userNumara!)" + " / Kat: " + "\(adresGelen.userKat!)" 
        cell.checkBox.tag = indexPath.row

        //cell.checkBox.addTarget(self, action: #selector(siparisOnayiController.onValueChanged(sender:)), for: UIControl.Event.valueChanged)
        cell.checkBox.addTarget(self, action: #selector(siparisOnayiController.onValueChanged(sender:)), for: UIControl.Event.valueChanged)
        return cell
    }
    else {

        let cell = tableView.dequeueReusableCell(withIdentifier: "teslimAdressCell", for: indexPath) as! teslimAdressCell
        let adresGelen = self.userAdressDictForTeslim[indexPath.item]
        cell.adresLabel.text = "\(adresGelen.userIlce!)" + " / " + "\(adresGelen.userMahalle!)" + " / " + "\(adresGelen.userSokak!)" + " / " + "\(adresGelen.userApartman!)" + " / No: " + "\(adresGelen.userNumara!)" + " / Kat: " + "\(adresGelen.userKat!)"
        cell.checkBox.tag = indexPath.row
        cell.checkBox.addTarget(self, action: #selector(siparisOnayiController.onValueChanged(sender:)), for: UIControl.Event.valueChanged)
        return cell


    }

}

Button tags only let you to pass an Int which is not enough for someone who wants both rows and sections (like your case).

In your cell's class define a closure:

var action : (() -> Void)? = nil

Then create an action func for your checkBox button (or whatever it is) in the cell's class:

@IBAction func btnAction(sender: M13Checkbox)
    {
    switch sender!.checkState {
        case .checked:
            if let act = self.action
            {
                act()
            }
            break
        case .unchecked:
            print("adres kaldırıldı bile...")
        break

        case .mixed:
            //empty...
        break
    }

}

Then in cellForRow func pass the closure:

cell.action = {
            //here you have access to indexPath.section and indexPath.row
            let adresso = self.userAdressDict[indexPath.row]      // teslimde calismaz suanda..
            print("\(adresso.userIlce!)" + " / " + "\(adresso.userMahalle!)" + " / " + "\(adresso.userSokak!)" + " / " + "\(adresso.userApartman!)" + " / No: " + "\(adresso.userNumara!)" + " / Kat: " + "\(adresso.userKat!)")

            let adresAlim = ("\(adresso.userMahalle!)" + " / " + "\(adresso.userSokak!)" + " / " + "\(adresso.userApartman!)")
            secilenAdresForAlim = adresAlim
            let deadlineTime = DispatchTime.now() + .milliseconds(500)
            DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
                if self.keys[0] == 0 {

                    self.keys[0] = 1
                }else{
                    self.keys[0] = 0
                    self.buttonLabel = "Değiştir"
                    self.headerTitleForSecZero = self.secilenAdresForAlim
                }
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
                }
              }

This is way more appropriate than determining the cell by tag. Now when you're passing the closure you have access to both indexPath.row and indexPath.section within the closure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM