2016-07-19 19 views
0

私は約5セクションのUITableViewを持っています。私は、ボタンをクリックしてこれらのセクションの1つを折りたたんで展開しようとしていますが、私が使用するコードが他のセクションの折りたたみを引き起こすという問題が発生しています。具体的には、すべての表示セクションの最初の行が折りたたまれています。Swift/iOS:UITableViewのセクションを折りたたむ

func didClickSectionCollapseButton() { 
    shouldCollapseSection = !shouldCollapseSection 
    tableView.beginUpdates() 
    tableView.reloadSections(NSIndexSet(index: 1), withRowAnimation: .Fade) 
    tableView.endUpdates() 
} 

そしてここnumberOfRowInSection方法です:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    switch section { 
    case 0: 
     return 1 
    case 1: 
     // collapsible section 
     return shouldCollapse ? 0 : collapsibleSectionCellCount 
    case 2: 
     return getCellCount() 
    case 3: 
     return 1 
    case 4: 
     return 1 
    default: 
     return 0 
    } 
} 

私はここに欠けているものがあります。ここ

は、そのコードがどのように見えるのですか?私は様々なチュートリアルや質問をしましたが、私はまだ解決策を見つけることができませんでした。

答えて

0

あなたは使用することができます。

func didClickSectionCollapseButton() { 
    shouldCollapseSection = !shouldCollapseSection 
    tableView.beginUpdates() 
    tableView.deleteSections(NSIndexSet(index: 1), withRowAnimation: .Fade) 
    tableView.endUpdates() 
} 
+0

は、ああ、それは私が当然のセクション数を更新する予定 –

+0

:(動作しません。しかし、私は唯一の非表示にしたいので、この解決策は私のために動作しないでしょうこのセクションの行には、ヘッダーとフッターが表示されます。崩壊を引き起こすボタンは、このヘッダー内にあります。 –

+0

?あなたが取得しているどのようなエラー –

0

beginUpdates()endUpdates()ペアで作品をあなたが、その後の挿入、削除、および選択操作をしたい場合は、なくreloadDataため。
コードにbeginUpdates()endUpdates()を削除してください。

+0

ええと、これはうまくいかないようです。 –

0

ボタンアクションで設定されるshouldCollapseSection変数とnumberOfRowsInSectionメソッドで使用されるshouldCollapse変数には違いがありますか?

データソースデリゲートで使用しているのと同じ変数を設定していないようです。

0

私は長い間、このコードをSwift 2.3に使用しています。これが助けになるかどうかは分かりませんが、言及する価値はあります。

class DriversVC : UIViewController , UITableViewDelegate , UITableViewDataSource { 
    //----------------------------------------------------------------------- 
    //MARK: - Outlets 

    @IBOutlet var tvDriverList: UITableView! { 
     didSet { 
      tvDriverList.delegate = self 
      tvDriverList.dataSource = self 
     } 
    } 

    //----------------------------------------------------------------------- 
    //MARK: - Variables 

    var arrDriverList : NSArray? //Section data 
    var arrWorkerList : NSArray? //Section data 
    var collapseSection0 : Bool = false 
    var collapseSection1 : Bool = false 
    var btnSection0Headder : UIButton = UIButton() 
    var btnSection1Headder : UIButton = UIButton() 

    //------------------------------------------------------ 

    func btnSection0HeadderTapped() { 
     if collapseSection0 { 
      collapseSection0 = false 
     } else { 
      collapseSection0 = true 
     } 
     tvDriverList.reloadSections(NSIndexSet(index: 0), withRowAnimation: UITableViewRowAnimation.Fade) 
    } 

    //------------------------------------------------------ 

    func btnSection1HeadderTapped() { 
     if collapseSection1 { 
      collapseSection1 = false 
     } else { 
      collapseSection1 = true 
     } 
     tvDriverList.reloadSections(NSIndexSet(index: 1), withRowAnimation: UITableViewRowAnimation.Fade) 
    } 

    //----------------------------------------------------------------------------------- 
    //MARK:- Table delegate and data sources 

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return 50 
    } 

    //------------------------------------------------------ 

    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
     return 20 
    } 

    //------------------------------------------------------ 

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
     let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 50)) 
     view.backgroundColor = OrangeColor //Set your color 
     let lbl = UILabel(frame: CGRect(x: 10, y: 5, width: UIScreen.mainScreen().bounds.width - 20, height: 40)) 
     lbl.font = UIFont(name: OpenSansRegular, size: 18) //Set your font 
     lbl.textColor = UIColor.whiteColor() 
     view.addSubview(lbl) 
     if section == 0 { 
      lbl.text = "D R I V E R" 
      btnSection0Headder.addTarget(self, action: #selector(self.btnSection0HeadderTapped), forControlEvents: .TouchUpInside) 
      btnSection0Headder.frame = view.frame 
      view.addSubview(btnSection0Headder) // uncomment to apply collapse effect 
     } else { 
      lbl.text = "W O R K E R" 
      btnSection1Headder.addTarget(self, action: #selector(self.btnSection1HeadderTapped), forControlEvents: .TouchUpInside) 
      btnSection1Headder.frame = view.frame 
      view.addSubview(btnSection1Headder) // uncomment to apply collapse effect 
     } 
     return view 
    } 

    //------------------------------------------------------ 

    func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 
     return UIView() 
    } 

    //------------------------------------------------------ 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     if arrWorkerList != nil && arrWorkerList?.count > 0 { 
      return 2 
     } 
     return 1 
    } 

    //------------------------------------------------------ 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if section == 0 { 
      if !collapseSection0 { 
       guard arrDriverList != nil else {return 0} 
       return arrDriverList!.count 
      } else { 
       return 0 
      } 
     } else { 
      if !collapseSection1 { 
       guard arrWorkerList != nil else {return 0} 
       return arrWorkerList!.count 
      } else { 
       return 0 
      } 
     } 
    } 

    //------------------------------------------------------ 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     guard let cell = tableView.dequeueReusableCellWithIdentifier(NSStringFromClass(DriversCVC).componentsSeparatedByString(".").last!) as? DriversCVC else { fatalError("unexpected DriversCVC dequeued from tableView") } 
     cell.superViewController = self 
     if indexPath.section == 0 { 
      guard let dict = arrDriverList![indexPath.row] as? NSDictionary else {return cell} 
      cell.data = dict 
     } else { 
      guard let dict = arrWorkerList![indexPath.row] as? NSDictionary else {return cell} 
      cell.data = dict 
     } 
     cell.setup() 
     return cell 
    } 

    //---------------------------------------------------------------------- 
    //MARK: - Action Method 

    @IBAction func btnBackTapped(sender: AnyObject) { 
     guard self.navigationController != nil else { 
      self.dismissViewControllerAnimated(true, completion: nil) 
      return 
     } 
     guard self.navigationController?.popViewControllerAnimated(true) != nil else { 
      guard self.navigationController?.dismissViewControllerAnimated(true, completion: nil) != nil else { 
       AppDelegate.sharedInstance().loginCall() 
       return 
      } 
      return 
     } 
    } 

    //----------------------------------------------------------------------- 
    //MARK: - View Life Cycle Methods 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

    //---------------------------------------------------------------------- 

    override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(animated) 
     setUpVC() 
    } 

    //---------------------------------------------------------------------- 

    override func viewDidAppear(animated: Bool) { 
     super.viewDidAppear(animated) 
    } }