Swift 5 UITableViewCell:展开一个部分并折叠展开的部分

时间:2020-05-14 09:43:31

标签: ios swift uitableview sections

我已经实现了以下代码,以向UITableView部分添加展开/折叠功能。当用户单击每个section1时,它会展开,而当我们单击相同的section1时,它会折叠。但是,如果要扩展section2,我希望section1能够折叠。如何在下面添加的代码中实现此功能。

struct FaqData{
    var faqHead = String()
    var faqImage = String()
    var questionArray : [(question : String, answer : String, answerurl : String)] = [(String,String,String)]()
    var openSection = Bool()
}

var supportArray = [FaqData]()

func numberOfSections(in tableView: UITableView) -> Int {
        return supportArray.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0{
            return 1
        }
        else{
            if supportArray[section].openSection == true{
                return supportArray[section].questionArray.count + 1
            }else{
                return 1
            }

        }


    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none

        if indexPath.section == 0{
            let cell = tableView.dequeueReusableCell(withIdentifier: "SupportCenterID", for: indexPath) as! SupportCenterTableViewCell
            cell.selectionStyle = UITableViewCell.SelectionStyle.none
            cell.faqCollection.reloadData()
            return cell
        }
        else{

            if indexPath.row == 0{

                let cell = tableView.dequeueReusableCell(withIdentifier: "SupportFaqID") as! SupportCenterFaqTableViewCell
                cell.selectionStyle = UITableViewCell.SelectionStyle.none
                let faqHead = supportArray[indexPath.section].faqHead
                cell.imageText.text = faqHead.capitalized
                cell.imageButton.setImage(UIImage(named: supportArray[indexPath.section].faqImage), for: .normal)

                return cell

            }
            else{

                let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionID") as! SupportQuestionTableViewCell
                cell.selectionStyle = UITableViewCell.SelectionStyle.none
                cell.isSelected = true
                cell.questionLabel.text = "Q.\(indexPath.row) " + supportArray[indexPath.section].questionArray[indexPath.row - 1].question
                cell.answerLabel.text = supportArray[indexPath.section].questionArray[indexPath.row - 1].answer
                print(supportArray[indexPath.section].questionArray[indexPath.row - 1].answerurl)
                if supportArray[indexPath.section].questionArray[indexPath.row - 1].answerurl == ""{
                    cell.urlButton.isHidden = true
                }
                else{
                    cell.urlButton.isHidden = false
                }
                cell.urlButton.isHidden = true
                cell.urlButton.tag = indexPath.row
                UserDefaults.standard.set(indexPath.section, forKey: "SectionValue")
                cell.urlButton.addTarget(self, action: #selector(urlButtonClicked(_:)), for: .touchUpInside)
                cell.layoutMargins = UIEdgeInsets.zero

                return cell

            }

        }


    }

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if supportArray[indexPath.section].openSection == true{

            if indexPath.section != 0{
                if indexPath.row == 0{
                    let cell = tableView.cellForRow(at: indexPath) as! SupportCenterFaqTableViewCell
                    cell.faqView.backgroundColor = .white
                    cell.imageButton.tintColor = UIColor(hexString: "#D71B61")
                    cell.imageText.textColor = UIColor(hexString: "#D71B61")
                }
            }

            supportArray[indexPath.section].openSection = false
            let sections = IndexSet.init(integer: indexPath.section)
            tableView.reloadSections(sections, with: .fade)

        }
        else{
            supportArray[indexPath.section].openSection = true
            let sections = IndexSet.init(integer: indexPath.section)
            tableView.reloadSections(sections, with: .fade)
            if indexPath.section != 0{
                if indexPath.row == 0{
                    let cell = tableView.cellForRow(at: indexPath) as! SupportCenterFaqTableViewCell
                    cell.faqView.backgroundColor = UIColor(hexString: "#D71B61")
                    cell.imageButton.tintColor = .white
                    cell.imageText.textColor = .white

                }
            }

        }

    }
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }

有人可以为此提供解决方案吗?

1 个答案:

答案 0 :(得分:1)

使用didselecterow方法执行此操作。这是您的情况的else情况

 // You will need to reload multiple sections. So make an array.
            var reloadSections = [Int]()
            // find already opened array
            if let alreadyOpenSection = supportArray.firstIndex(where: { (faq) -> Bool in
                return faq.openSection
            }) {
                // if found, toggle the openSections bit
                supportArray[alreadyOpenSection].openSection = false
                // add it to reload sections array
                reloadSections.append(alreadyOpenSection)
            }
            supportArray[indexPath.section].openSection = true
            reloadSections.append(indexPath.section)
            // create index set with reload sections array
            let sections = IndexSet.init(reloadSections)
            tableView.reloadSections(sections, with: .fade)
            // below code is same
            if indexPath.section != 0{
                if indexPath.row == 0{
                    let cell = tableView.cellForRow(at: indexPath) as! SupportCenterFaqTableViewCell
                    cell.faqView.backgroundColor = UIColor(hexString: "#D71B61")
                    cell.imageButton.tintColor = .white
                    cell.imageText.textColor = .white

                }
            }