这是我用来展开和折叠标题部分的代码。试图只允许扩展一个部分。因此,当用户点击另一个部分时,它会首先折叠已展开的部分,然后展开新的部分。请分享我一次只能扩展一个部分的信息。当我使用这段代码时,它不能像上面那样工作。它打开多个部分,而不关闭前面的部分。
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/