首页 > 解决方案 > 如何快速制作多行表格视图标题?

问题描述

我对表格视图中的多行标题有疑问。我已经尝试过了lineBreakMode = .byWordWrappingnumberOfLines = 0我也设置了

tableView.sectionHeaderHeight = UITableView.automaticDimension tableView.estimatedSectionHeaderHeight = 44

但是这些解决方案对我没有帮助。如何为我的表格视图制作多行标题?

这是我的标题视图代码:

   func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        if section == 0 { 
            let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 100))
            // code for adding centered title
            headerView.backgroundColor = UIColor(red: 225/255.0, green: 225/255.0, blue: 225/255.0, alpha: 1.0)
            let headerLabel = UILabel(frame: CGRect(x: 10, y: 8, width: tableView.bounds.size.width - 120, height: 28))
            headerLabel.textColor = UIColor.black
            headerLabel.lineBreakMode = .byWordWrapping
            headerLabel.numberOfLines = 0
            headerLabel.text = headerTitleForSecZero
            headerLabel.font = UIFont.boldSystemFont(ofSize: 14)
            headerLabel.textAlignment = .left
            headerView.addSubview(headerLabel)

            button.frame = CGRect(x:headerView.frame.size.width - 85, y:8, width:70, height:28)
            button.setTitle(buttonLabel,for: .normal)
            button.tag = 1
            button.addTarget(self, action: #selector(handleExpandCloseForAlim(sender:)), for: .touchUpInside)

            adresDegistirmeButonu.frame = CGRect(x:headerView.frame.size.width - 100, y:8, width:90, height:28)

            headerView.addSubview(adresDegistirmeButonu)
            headerView.addSubview(button)
            return headerView
        }else{
            return nil
        }
    }     

标签: iosswiftuitableview

解决方案


正如我在评论中提到的,使用允许文本标签增长的 AutoLayout 约束构建标题视图应该会给您所需的灵活性。下面的示例只是使用了一个多行文本的虚拟数组来填充标题视图。它还将页脚设置为不显示,因为空页脚会混淆布局。

class MyVC1: UIViewController {
   var tableView: UITableView!
   override func viewDidLoad() {
      super.viewDidLoad()
      addTableView()
   }

   func addTableView() {
      tableView = UITableView(frame: .zero, style: .grouped)
      tableView.delegate = self
      tableView.dataSource = self
      view.addSubview(tableView)
      tableView.translatesAutoresizingMaskIntoConstraints = false
      tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
      tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
      tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
      tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
      tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
   }
}

extension MyVC1: UITableViewDelegate, UITableViewDataSource {
   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      5
   }

   func numberOfSections(in tableView: UITableView) -> Int {
      3
   }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
      cell.textLabel?.text = "Section: \(indexPath.section), Row: \(indexPath.row)"
      return cell
   }

   func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  //set up dummy header text
      var text = [String]()
      text.append( "One Line")
      text.append("""
      Three Lines
      Three Lines
      Three Lines
      """)
      text.append("""
      Two Lines
      Two Lines
      """)


      let header = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 100))
      let label = UILabel()
      label.numberOfLines = 0
      let button = UIButton(type: .custom)
      button.setTitle("Press Me", for: .normal)
      button.setTitleColor(.purple, for: .normal)
      button.backgroundColor = .lightGray
      header.addSubview(label)
      header.addSubview(button)
      label.translatesAutoresizingMaskIntoConstraints = false
      button.translatesAutoresizingMaskIntoConstraints = false
      NSLayoutConstraint.activate([
         label.topAnchor.constraint(equalTo: header.topAnchor, constant: 10),
         label.bottomAnchor.constraint(equalTo: header.bottomAnchor, constant: -10),
         label.leadingAnchor.constraint(equalTo: header.leadingAnchor, constant: 10),
         button.centerYAnchor.constraint(equalTo: label.centerYAnchor),
         button.widthAnchor.constraint(equalToConstant: 150),
         button.trailingAnchor.constraint(equalTo: header.trailingAnchor, constant: -10),
         label.trailingAnchor.constraint(greaterThanOrEqualTo: button.leadingAnchor, constant: -50)
      ])
      label.text = text[section]
      return header
   }

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

   func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
      0
   }
}

推荐阅读