ここでは、ヘッダーセクションを展開および折りたたむために使用しているコードを示します。 1つのセクションのみを展開できるようにしようとしています。したがって、ユーザーが別のセクションをタップすると、最初に展開されたセクションが折りたたまれ、次に新しいセクションが展開されます。一度に展開するセクションを1つだけ取得する情報を共有してください。このコードを使用すると、上記のように動作しません。これは、複数のセクションを開き、前のセクションを閉じません。Swiftを使用して1つのセクションのみをセクションヘッダーで展開できるようにする
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)
}
}
}
}
}
は、Uにモースありがとうございます。あなたの貴重な提案のためにsadiqしかし、私はあなたが言ったことをまったく同じに従ったが、それは前のセクションを崩壊させませんでした。私は何を間違えているのか分からない –