这是我用来扩展和折叠标题部分的代码。试图只允许扩展一个部分。因此,当用户点击另一个部分时,它将首先折叠展开的部分,然后展开新部分。请分享我一次只能展开一个部分的信息。当我使用此代码时,它不像上面那样工作。它会打开多个部分,而不会关闭前面的部分。
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)
}
}
}
}
}
答案 0 :(得分:0)
维护indexOfPreviouslyExpanded部分。 为它开始分配-1。在您的方法中检查是否有一些以前扩展的部分。除了当前扩展的部分之外,您还需要重新加载indexOfPreviouslyExpanded的部分。每次选择一个部分时,还要确保更新indexOfPreviouslyExpanded。
答案 1 :(得分:0)
试试这个: -
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.
}
}
}
}
}