如何使tableview部分保持状况?

时间:2019-10-22 14:18:27

标签: ios swift xcode

我有两个部分,这些单元格都有复选框。这些复选框控制tableview扩展动作。在我的代码中,当我单击零部分复选框时,零部分复选框工作良好,但是当我尝试对第一部分进行相同操作时,由于无法进行部分检测而出现错误,当我单击第一部分复选框时,它继续关闭零区。如何在动作函数中访问tableview部分?

这是我的复选框动作功能代码:

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


}

这是我的cellForRow函数代码:

        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


    }

}

1 个答案:

答案 0 :(得分:0)

按钮标记仅允许您传递Int,对于需要行和节的人(例如您的情况)来说,这是不够的。

在您的单元格类中定义一个闭包:

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

然后在单元格的类中为您的checkBox按钮(或其他按钮)创建一个动作函数:

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

}

然后在cellForRow函数中通过闭包:

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()
                }
                }
              }

这比按标签确定单元格更合适。现在,当您传递闭包时,您可以同时访问闭包内的indexPath.rowindexPath.section