ios - 使用 Swift 只允许在节标题中展开一个节

标签 ios swift header 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)

                    }
                }
            }
        }
    }                                    

最佳答案

维护indexOfPreviouslyExpanded部分。 首先为其指定 -1。在您的方法中检查是否有一些先前扩展的部分。除了当前扩展的部分之外,您还需要重新加载 indexOfPreviouslyExpanded 的部分。还要确保每次选择一个部分时都更新indexOfPreviouslyExpanded。

关于ios - 使用 Swift 只允许在节标题中展开一个节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45836204/

相关文章:

ios - 如何让函数接受所有类型的数组

php - 无法在 PHPStorm 中运行 PHPUnit 测试

c++ - 在 C++ 中读取 pcap 文件

c - 在 TCP header 中操作 ISN 号的最有效方法

ios - 按钮动画无法在 swift 中正常工作

ios - 是否可以重置/丢弃 Firebase Remote Config 获取的值?

ios - CPF 和 CNPJ 的格式文本字段

objective-c - App 日历功能不再适用于 ios 6

swift - Swift 2 错误 : Cannot assign a value of type '[NSURL]' to a value of type '[NSURL]'

c# - 如何将基于 swift 的 cocoapods 移植到 Xamarin