【问题标题】:How to add footer as additional information in table view swift如何在表格视图中快速添加页脚作为附加信息
【发布时间】:2020-10-19 12:52:20
【问题描述】:

我想创建一个页脚作为附加信息。是这样的:

我试图给出一个页脚,但它显示了一个标题并且它是粗体的。 此页脚的字体较小,页脚的背景就像在表格之外。 我该怎么做?

这是我的一些代码

override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView()
    if section == 1 {
        let label = UILabel()
        label.text = "Additional information here"
        label.font = .systemFont(ofSize: 16)
        label.textColor = UIColor.black
        label.backgroundColor = UIColor.clear
        label.textAlignment = .left
        footerView.addSubview(label)
    }
    return footerView
}

更新:

我正在获取屏幕最左侧的边距。

任何帮助将不胜感激

谢谢

【问题讨论】:

  • 你现在得到了什么?

标签: ios swift header tableview footer


【解决方案1】:

使用viewForFooterInSection 返回自定义UILabel

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let label = UILabel()
    label.numberOfLines = 0
    label.text = "Your text"
    label.textColor = .gray
    return label
}

【讨论】:

    【解决方案2】:

    您可以通过对UILabel 设置约束来修复您的代码:

    let footerView = UIView()
    if section == 1 {
      let label = UILabel()
      label.text = "Additional information here"
      label.font = .systemFont(ofSize: 16)
      label.textColor = UIColor.black
      label.backgroundColor = UIColor.clear
      label.textAlignment = .left
      footerView.addSubview(label)
      label.translatesAutoresizingMaskIntoConstraints = false
      label.topAnchor.constraint(equalTo: footerView.topAnchor).isActive = true
      label.leftAnchor.constraint(equalTo: footerView.leftAnchor).isActive = true
      label.rightAnchor.constraint(equalTo: footerView.rightAnchor).isActive = true
      label.bottomAnchor.constraint(equalTo: footerView.bottomAnchor).isActive = true
    }
    return footerView
    

    但是直接使用UILabel会更简单,因为UILabel也是UIView,你可以直接返回。

    【讨论】:

    • 嘿伙计,你能看看我更新的帖子吗?我曾尝试使用您的锚点,但它给了我屏幕最左侧的页脚。我希望我的左边距有 +20。我怎么做?提前谢谢你
    • 您可以通过以下方式实现此目的:label.leftAnchor.constraint(equalTo: footerView.leftAnchor, constant: 20).isActive = true
    【解决方案3】:

    您可以使用heightForFooterInSectionviewForFooterInSection 委托在 TableView 中添加页脚视图。您可以自定义 UILabel 的值。

    仅为要添加页脚视图的相关部分创建并返回标签和相关高度。

    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return section == 1 ? 20 : CGFloat.Magnitude.leastNonzeroMagnitude
    }
    
    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        if section != 1 { return nil }
        let label = UILabel()
        label.numberOfLines = 0
        label.text = "Additional information here"
        // customize font and colors.
        label.font = .systemFont(ofSize: 16)
        label.textColor = UIColor.black
        label.backgroundColor = UIColor.clear
        label.textAlignment = .left
        return label
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      • 2015-01-02
      相关资源
      最近更新 更多