ヘッダーをタップして拡張可能なテーブルビューを作成したいのですが、ヘッダーをインスタンス化する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
}
tableviewコードのnumberofsectionsは何ですか? – hariszaman
func numberOfSectionsInTableView(tableView:UITableView) - > Int { 返信data.count }この部分はうまくいきます –
何かについては、 – hariszaman