【问题标题】:Adding footer to UITableView向 UITableView 添加页脚
【发布时间】:2021-07-06 13:23:53
【问题描述】:

所以,我制作了一个带有自定义单元格(.xib 文件)的 UITableView,效果很好。问题是我现在想添加一个页脚,它需要一个 UIImageView 和一个 UILabel。

这就是我目前正在做的事情:(当然我在 viewDidLoad() 中执行这个函数)

private func setupTableViewFooter() {
        /// Insight Logo
        let myAppLogo = UIImageView()
        myAppLogo.backgroundColor = .clear
        myAppLogo.widthAnchor.constraint(equalToConstant: 110.0).isActive = true
        myAppLogo.heightAnchor.constraint(equalToConstant: 60.0).isActive = true
        myAppLogo.contentMode = .scaleAspectFit
        myAppLogo.image = UIImage(named: "myAppLogo")
        
        /// Description Label
        let label = UILabel()
        label.backgroundColor = .clear
        label.widthAnchor.constraint(equalToConstant: self.view.frame.size.width - 60).isActive = true
        label.heightAnchor.constraint(equalToConstant: 30.0).isActive = true
        label.textAlignment = .left
        label.textColor = K.Colors.black
        label.font = UIFont(name: "Roboto-Medium", size: 13)
        label.text = "Hello world!"
        
        /// Stack View
        let footer = UIStackView()
        footer.axis = .vertical
        footer.distribution = .fill
        footer.alignment = .leading
        footer.spacing = 00.0
        footer.backgroundColor = .lightGray
        
        footer.addArrangedSubview(myAppLogo)
        footer.addArrangedSubview(label)
        footer.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            footer.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 30),
            footer.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -30),
            footer.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -60),
            footer.heightAnchor.constraint(equalToConstant: 90)
        ])
        
        self.tableView.tableFooterView = footer
    }

我收到此错误:

libc++abi.dylib:以 NSException 类型的未捕获异常终止 *** 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“无法使用锚点激活约束 <0x282fa0640><0x282f56400>

标签: ios swift autolayout constraints


【解决方案1】:

首先将fotter添加到tableView然后添加Constraints

100% 正常测试

self.tableView.tableFooterView = footer

NSLayoutConstraint.activate([ footer.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 30), footer.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -30), footer.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -60), footer.heightAnchor.constraint(equalToConstant: 90) ])

【讨论】:

  • 嘿它解决了错误,谢谢!但它把页脚作为页眉.. 奇怪
  • 我刚刚注意到它不仅被放置为标题,而且还被放置在我的前 2 个单元格的前面(看不到它们)。
  • 我终于解决了这个奇怪的行为,通过创建一个方法,它返回的不是UIStackView,而是一个UIView,其中包含已应用约束的图像和标签(两者都带有translatesAutoresizingMaskIntoConstraintsUIView 上设置为false)。之后,我使用UITableViewDelegate 方法将视图作为页脚返回:viewForFooterInSection,并使用heightForFooterInSection 设置其高度。 Pd:我将把你的答案标记为解决方案,因为它解决了我询问的错误,并导致我按照我想要的方式设置页脚。
猜你喜欢
  • 2011-07-05
  • 1970-01-01
  • 2012-03-25
  • 2022-01-23
  • 1970-01-01
  • 2011-01-31
  • 2016-11-05
  • 2021-05-24
  • 1970-01-01
相关资源
最近更新 更多