ios - 对齐 UITableViewHeaderFooterView 的 detailTextLabel

标签 ios swift uitableview autolayout uitableviewsectionheader

我有一个 UITableView 并且想自定义它的 sectionHeader。我的目标是使用自动布局使 detailTextLabel 右对齐。 (见照片)。许多小时我试图设置它的 anchor ,修改 tableview.sectionHeaderHeight,使用 Headers contentView 但我没有得到它..

有人可以告诉我如何正确管理它吗?我应该创建自定义 UIView 然后将其添加到 UITableViewHeaderFooterView 吗?那么如何获取标签的字体属性呢?

Illustration 使用 Keynote 创建


我的代码看起来像这样,但我不会真的以此为起点.. :)

class GSTableHeaderView: UITableViewHeaderFooterView, ReusableView {

  override init(reuseIdentifier: String?) {
    super.init(reuseIdentifier: reuseIdentifier)
  }

  func configure() {
    setupSubviews()
    setupConstraints()
  }

  func setupSubviews() {
    guard let detailTextLabel = detailTextLabel, let textLabel = textLabel else { return }
    contentView.addSubview(detailTextLabel)
  }

  func setupConstraints() {
    guard let detailTextLabel = detailTextLabel, let textLabel = textLabel else { return }
    detailTextLabel.anchor(top: nil, leading: nil, bottom: contentView.bottomAnchor, trailing: contentView.trailingAnchor, paddingTop: 0, paddingLeading: 0, paddingBottom: 0, paddingTrailing: 16, width: 0, height: 0)
    detailTextLabel.centerYAnchor.constraint(equalTo: textLabel.centerYAnchor).isActive = true
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

lazy var tableView: UITableView = {
    let tableview = UITableView(frame: .zero, style: .grouped)
    tableview.dataSource = self
    tableview.delegate = self
    tableview.showsHorizontalScrollIndicator = false
    tableview.rowHeight = 44
    tableview.sectionHeaderHeight = 70
    tableview.estimatedSectionHeaderHeight = 70
    tableview.isScrollEnabled = false
    tableview.isUserInteractionEnabled = true
    tableview.register(GSAltDetailTableViewCell.self, forCellReuseIdentifier: GSAltDetailTableViewCell.reuseIdentifier)
    tableview.register(GSTableHeaderView.self, forHeaderFooterViewReuseIdentifier: GSTableHeaderView.reuseIdentifier)
    return tableview
}()

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: GSTableHeaderView.reuseIdentifier) as! GSTableHeaderView
    header.textLabel?.text = "Details"
    header.detailTextLabel?.text = "Expand all"
    header.tag = section

    header.configure()
    return header
}

最佳答案

使用 xib 文件创建 CustomSectionHeaderView: enter image description here

你的类(class):

class CustomSectionHeaderView: UIView {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var detailLabel: UILabel!

    class func fromNib() -> CustomSectionHeaderView {
        return UINib(nibName: String(describing: self), bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomSectionHeaderView
    }

    override func awakeFromNib() {
        super.awakeFromNib()

    }
}

在 ViewContoller 中:

var headerSectionView = CustomSectionHeaderView.fromNib()

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

    headerSectionView.titleLabel.text = "TITLE"
    headerSectionView.detailLabel.text = "DETAILS"

    //or if you need 
    //headerSectionView.detailLabel.isHidden = true

    return headerSectionView
}

关于ios - 对齐 UITableViewHeaderFooterView 的 detailTextLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52816918/

相关文章:

ios - iOS 6应用在iOS7中的外观/效果很好-在XCode 5中看起来很恐怖

iphone - If 语句不能正确使用 indexpath

javascript - 如果 ssl (https) 证书无效,React Native XMLHttpRequest 请求将失败

ios - 无法将类型 '() -> _' 的值转换为指定类型的 UIImageView

ios - 运行时的 Swift UITableViewCell 动态单元格高度

ios - UITableViewCell 中的 UIStepper 在点击时出现问题

ios - 如何动态添加新单元格到UICollectionView?

ios - 将麦克风音频从 iPhone 传输到外部服务器

swift - 如何确保新的 View Controller 对用户可见?

ios - 如何用一次性发布者包装委托(delegate)模式?