2016-04-12 13 views
0

ヘッダーをタップして拡張可能なテーブルビューを作成したいのですが、ヘッダーをインスタンス化するviewForHeaderInSectionでheaderfooterviewクラスが呼び出されています。セクションが折りたたまれると、サブビューが消えます。コードセクションをリロードするときにテーブルビューヘッダーのサブビューが消え去る

override func viewDidLoad() { 

    super.viewDidLoad() 

    tableView.registerClass(CustomHeaderTableViewCell.self, forHeaderFooterViewReuseIdentifier: "header") 
    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 

     return data.count 

    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 


    if section == selectedSection { 
     return 0 
     } else { 
     return data[section].count 
     }   
    } 

    func expandCollapse(sender: UIButton) { 

     self.selectedSection = sender.tag 
     tableView.reloadSections(NSIndexSet(index: sender.tag), withRowAnimation: .None) 

    } 
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
     let header: CustomHeaderTableViewCell = tableView.dequeueReusableHeaderFooterViewWithIdentifier("header") as! CustomHeaderTableViewCell 
     let button = UIButton(type: UIButtonType.System) as UIButton 
     button.frame = header.bounds 
     button.backgroundColor = UIColor.greenColor() 
     button.setTitle("Test Button", forState: UIControlState.Normal) 
     button.addTarget(self, action: #selector(MainWitness.expandCollapse), forControlEvents: UIControlEvents.TouchUpInside) 
     button.tag = section 
     header.addSubview(button) 

     header.textLabel!.text = "TRENDS" 

     return header 
    } 
+0

tableviewコードのnumberofsectionsは何ですか? – hariszaman

+0

func numberOfSectionsInTableView(tableView:UITableView) - > Int { 返信data.count }この部分はうまくいきます –

+0

何かについては、 – hariszaman

答えて

0

代わりのプロトタイプセルからヘッダを移入の

ここ

Sビットは、ヘッダビューの別.XIBを作成してからヘッダビューを取り込みます。それが動作します。

+0

UITableViewCellはUIVIewのサブクラスなので重要ですか? – hariszaman

+0

はい、あなたは正しいですが、UITableViewCellはUIViewとして使われることを意図していません。再使用可能なセルからヘッダビューを作成する際に問題があります。私は同じ問題に直面し、.XIBは私を救った。 –

+0

私はnoobのように見える場合は申し訳ありませんが、私はそれを取得しない、あなたは別の.xibを使用してどういう意味ですか?ここでは、headerfooterviewクラスのヘッダにプロトタイプのセルを挿入していますが、他の方法でそれを行う方法はわかりません。 –

関連する問題