【发布时间】:2020-01-19 11:40:58
【问题描述】:
您好,我尝试使用 UITableView 进行展开/折叠,但标题有问题。我已经尝试了所有教程但没有工作。这是我的示例代码:
导入 UIKit 导入 SafariServices 类 AboutViewController: UIViewController { @IBOutlet 弱变量 tblView:UITableView! 变量数据 = [ DataModal(headerName: "Apa itu Brevir?", subType: ["Brevir adalah bla..bla"], isExpandable: false), DataModal(headerName: "Apa isi Brevir?", subType: ["Garis besarnya adalah bla..bla..blaa...bla..bla..blaa...bla..bla..blaa...bla. .bla..blaa"], isExpandable: false), DataModal(headerName: "Mengapa 7x Sehari?", subType: ["Tujuh Kali dalam bla..bla"], isExpandable: false), DataModal(headerName: "Ibadat apa saja yang termaksud dalam 7x sehari tersebut?", subType: ["a. Ibadat Pembukaan", "b. Ibadat Pembukaan", "c. Ibadat Pembukaan", "d. Ibadat Pembukaan", "e .Ibadat Pembukaan", "f. Ibadat Pembukaan", "g. Ibadat Pembukaan"], isExpandable: false)] 覆盖 func viewDidLoad() { super.viewDidLoad() tblView.tableFooterView = UIView() } } 扩展 AboutViewController: UITableViewDataSource, UITableViewDelegate { func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 让 headerView = HeaderView(frame: CGRect(x: 10, y: 10, width: tblView.frame.size.width - 20, height: 40)) headerView.delegate = self headerView.secIndex = 部分 headerView.btn.setTitle(data[section].headerName, for: .normal) 返回标题视图 } func numberOfSections(in tableView: UITableView) -> Int { 返回数据.count } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 返回 44 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 如果数据[节].isExpandable { 返回数据[节].subType.count } 别的 { 返回 Int(UITableView.automaticDimension) } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) cell.textLabel?.numberOfLines = 0 cell.textLabel?.text = data[indexPath.section].subType[indexPath.row] 返回单元格 } 覆盖 func viewWillAppear(_ animated: Bool) { tblView.estimatedSectionHeaderHeight = 40 tblView.sectionHeaderHeight = UITableView.automaticDimension } } 扩展 AboutViewController: HeaderDelegate { func callHeader(idx: Int) { 数据[idx].isExpandable = !data[idx].isExpandable tblView.reloadSections([idx], with: .automatic) } }【问题讨论】:
-
找到您的解决方案了吗?
标签: swift xcode uitableview autolayout