简体   繁体   中英

Allow only one section to be expand in section header using Swift

Here is my code that I am using to expand and collapse the header section. Trying to allow only one section to be expanded. So when the user taps another section, it will first collapse the expanded one, and then expand the new one. Please share the info by which I get only one section to be expand at one time. As I use this code, it's not working as above. It opens more than one section, not closing the previous sections.

func sectionHeaderTapped(_ gestureRecognizer: UITapGestureRecognizer) {
    let indexPath: IndexPath = IndexPath(row: 0, section: gestureRecognizer.view!.tag)

    if (indexPath as NSIndexPath).row == 0 {

        collapsed = CBool(arrayForBool[(indexPath as NSIndexPath).section] as! NSNumber)
        print("Collapsed: ",collapsed)

        for i in 0 ..< sortedListAry.count {

            if (indexPath as NSIndexPath).section == i {
                arrayForBool[i] = !collapsed
                print("arrayForBool: ",arrayForBool)
                if (arrayForBool[i] as! Bool) == true{                       
                    let abc = (self.subListAry.object(at: (indexPath as NSIndexPath).section) as AnyObject).value(forKey: "category_id") as! NSArray
                  if (abc.count == 0)
                    {
 //                        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
//                            let viewController = mainStoryboard.instantiateViewController(withIdentifier: "SubCategory") as! subCat
//                            viewController.subCategoryID = ((self.sortedListAry.object(at:(indexPath as NSIndexPath).section) as AnyObject).value(forKey: "category_id") as? String)!

 //                           self.navigationController?.pushViewController(ViewController, animated: true)

                    }


                    else{

                        collapsed = true
                                categoryListTbl.reloadSections(IndexSet(integer: gestureRecognizer.view!.tag), with: .automatic)
                        categoryListTbl.scrollToRow(at: indexPath, at: .top, animated: true)

                    }
                }

               else{

                    collapsed = false
                    categoryListTbl.reloadSections(IndexSet(integer: gestureRecognizer.view!.tag), with: .automatic)

                    }
                }
            }
        }
    }                                    

Maintain indexOfPreviouslyExpanded section. Assign -1 to it to start with. In your method check if there is some previously expanded section. You will need to reload section of indexOfPreviouslyExpanded in addition to current expanded section. Also make sure to update indexOfPreviouslyExpanded every time you select a section.

try this :-

func sectionHeaderTapped(_ gestureRecognizer: UITapGestureRecognizer) {
     let indexPath: IndexPath = IndexPath(row: 0, section: 
gestureRecognizer.view!.tag)

    if (indexPath as NSIndexPath).row == 0 {

        collapsed = CBool(arrayForBool[(indexPath as NSIndexPath).section] as! NSNumber)
        print("Collapsed: ",collapsed)

        for i in 0 ..< sortedListAry.count {

            if (indexPath as NSIndexPath).section == i {
                arrayForBool[i] = !collapsed
                print("arrayForBool: ",arrayForBool)
                if (arrayForBool[i] as! Bool) == true{
                    let abc = (self.subListAry.object(at: (indexPath as NSIndexPath).section) as AnyObject).value(forKey: "category_id") as! NSArray
                    if (abc.count == 0)
                    {
                        //                        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                        //                            let viewController = mainStoryboard.instantiateViewController(withIdentifier: "SubCategory") as! subCat
                        //                            viewController.subCategoryID = ((self.sortedListAry.object(at:(indexPath as NSIndexPath).section) as AnyObject).value(forKey: "category_id") as? String)!

                        //                           self.navigationController?.pushViewController(ViewController, animated: true)

                    }


                    else{

                        collapsed = true
                        categoryListTbl.reloadSections(IndexSet(integer: gestureRecognizer.view!.tag), with: .automatic)
                        categoryListTbl.scrollToRow(at: indexPath, at: .top, animated: true)

                    }
                }

                else{

                    collapsed = false
                    categoryListTbl.reloadSections(IndexSet(integer: gestureRecognizer.view!.tag), with: .automatic)

                }
            }else {

                // here collapse all other section replace your bool array with value which you are using to collapse your section and reload tableview.

            }
        }
    }
}

}

1.Create a model of sectionData and add boolean variable to manage state of particular section

2.while creating add default value to true if you want to initially in collapse status.

3.In No of row method return 0 if section header state is true else no of row based on current section

     if self.graphDataArray?[section].collapsed == true {
            return 0
        }

4.Then on section button click use for in loop to check status of each section state and chnage current clicked section state to true , rest state to false

  1. reload data of tableview or call reloadsection.

     for i in 0..<self.graphDataArray!.count { let currentSectionModel = self.graphDataArray?[i] if i == section { currentSectionModel?.collapsed = false sender.setImage(#imageLiteral(resourceName: "Minus"), for: .normal) sender.tintColor = hexStringToUIColor(hex: hexBlueColor0036b9) } else{ currentSectionModel?.collapsed = true sender.setImage(#imageLiteral(resourceName: "Plus"), for: .normal) sender.tintColor = hexStringToUIColor(hex: hexBlueColor0036b9) } } self.tblAnalysis.reloadData()

    //Happy coding

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