【问题标题】:How to make Automatic Height Header in UITableView?如何在 UITableView 中制作自动高度标题?
【发布时间】: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


【解决方案1】:

首先您必须计算高度并使用“UITableViewDelegate”方法,如下所示

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) 
   -> CGFloat {
return ( your calculated height for header)
 }

【讨论】:

    【解决方案2】:

    试试这个:

    
        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
            let board = UIView()
    
            let headerView = HeaderView(frame: CGRect(x: 10, y: 10, width: tablView.frame.size.width - 20, height: 40)
            headerView.delegate = self
            headerView.secIndex = section
            headerView.btn.setTitle(data[section].headerName, for: .normal)
            board.addSubview(headerView)
            return board
        }
    

    【讨论】:

      【解决方案3】:

      由于您使用的是按钮,它不会自动展开。

      改用标签并设置其numberOfLines = 0

      稍后在其上添加 Button。

      还实现表视图部分委托/数据源方法:

      heightForHeaderInSection return 50 //minimum height
      
      estimateHeightForHeaderSection return UITableView.automaticDimension // automatic height
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 2021-08-28
        • 1970-01-01
        • 1970-01-01
        • 2013-12-03
        • 2015-11-05
        • 1970-01-01
        相关资源
        最近更新 更多