【问题标题】:Update footer in UITableView更新 UITableView 中的页脚
【发布时间】:2016-12-13 10:08:55
【问题描述】:

在我的 UITableView 中有自定义页脚视图:

public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {   // custom view for footer. will be adjusted to default or specified footer height
    return footer()
}

func footer() -> UILabel {
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: (navigationController?.navigationBar.bounds.size.height)!))
    label.backgroundColor = AppColors.Bordo.color
    label.font = UIFont.boldSystemFont(ofSize: 16)
    label.textColor = .white
    label.text = "Selected \(self.selectedGenres.count) of \(self.genres.count)"
    label.textAlignment = .center
    return label
}

当用户在表格视图中选择/取消选择行时,我想用所选行的信息刷新页脚。如何在不重新加载整个 tableview 的情况下做到这一点? UITableView的footerView(forSection: indexPath.section)是做什么的?

【问题讨论】:

  • 只需致电reloadSections

标签: swift uitableview


【解决方案1】:

创建一个 label 的全局对象...并仅在 label 为 nil 时初始化它...并且您可以在代码中的任何位置访问此标签。

let globalLabel : UILabel ?

public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {   // custom view for footer. will be adjusted to default or specified footer height
    return footer()
}

func footer() -> UILabel {

    if (globalLabel == nil) {
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: (navigationController?.navigationBar.bounds.size.height)!))
        label.backgroundColor = AppColors.Bordo.color
        label.font = UIFont.boldSystemFont(ofSize: 16)
        label.textColor = .white
        label.textAlignment = .center
        globalLabel = label
    }

    globalLabel.text = "Selected \(self.selectedGenres.count) of \(self.genres.count)"

    return globalLabel
}

【讨论】:

    【解决方案2】:

    使用 Rajesh Choudhary 提议和计算属性完成它:

    var selectedGenres: [Genre] = [] {
        didSet {
            self.footerForTableView.text = titleForFooter
        }
    }
    
    var titleForFooter: String {
        return "Selected \(self.selectedGenres.count) of \(self.genres.count)"
    }
    
    lazy var footerForTableView: UILabel = {
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: (self.navigationController?.navigationBar.bounds.size.height)!))
        label.backgroundColor = AppColors.Bordo.color
        label.font = UIFont.boldSystemFont(ofSize: 16)
        label.textColor = .white
        label.text = self.titleForFooter
        label.textAlignment = .center
        return label
    }()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-21
      • 2018-03-13
      • 2014-03-10
      • 2011-02-12
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 2013-06-16
      相关资源
      最近更新 更多