2017-08-23 7 views
0

ここでは、ヘッダーセクションを展開および折りたたむために使用しているコードを示します。 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) 

        } 
       } 
      } 
     } 
    }          

答えて

0

インデックスを以前に拡張しました。 開始するには-1を割り当てます。あなたのメソッドで、以前に展開されたセクションがあるかどうかを確認します。現在展開されているセクションに加えて、indexOfPreviouslyExpandedのセクションをリロードする必要があります。また、セクションを選択するたびに必ずindexOfPreviouslyExpandedを更新してください。

+0

は、Uにモースありがとうございます。あなたの貴重な提案のためにsadiqしかし、私はあなたが言ったことをまったく同じに従ったが、それは前のセクションを崩壊させませんでした。私は何を間違えているのか分からない –

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. 

      } 
     } 
    } 
} 

}

関連する問題