【问题标题】:UITableView Section Header not showingUITableView 部分标题未显示
【发布时间】:2016-02-23 03:09:35
【问题描述】:

我正在尝试使用自动布局制作节标题。 带有标题和计数器的简单标题

class ProfilePeopleListHeaderViewCell: UIView {

  let titleLeftOffset = 12
  let counterRightOffset = 5
  let counterWidth = 50

  var title = "title" {
    didSet {
      titleLabel.text = title
    }
  }
  var numberOfPeople = 0 {
    didSet {
      peopleCounter.text = "\(numberOfPeople)"
    }
  }

  let titleLabel = UILabel()
  let peopleCounter = UILabel()


  convenience init(title: String, numberOfPeople:Int) {
    self.init()
    self.title = title
    self.numberOfPeople = numberOfPeople
    backgroundColor = UIColor.greenColor()
    titleLabel.textColor = Constants.Colors.ThemeGreen
    titleLabel.textAlignment = .Left
    if #available(iOS 8.2, *) {
      titleLabel.font = Constants.Fonts.Profile.PeopleListHeaderFont
      peopleCounter.font = Constants.Fonts.Profile.PeopleListHeaderFont
    } else {
      titleLabel.font = UIFont.systemFontOfSize(14)
      peopleCounter.font = UIFont.systemFontOfSize(14)
    }

    peopleCounter.textAlignment = .Right
    peopleCounter.textColor = Constants.Colors.ThemeDarkGray

    addSubview(titleLabel)
    addSubview(peopleCounter)
    self.titleLabel.snp_makeConstraints { (make) -> Void in
      make.centerY.equalTo(self)
      make.height.equalTo(self)
      make.width.equalTo(peopleCounter).multipliedBy(3)
      make.left.equalTo(self).offset(titleLeftOffset)
    }

    self.peopleCounter.snp_makeConstraints { (make) -> Void in
      make.centerY.equalTo(self)
      make.height.equalTo(self)
      make.width.equalTo(counterWidth)
      make.left.equalTo(titleLabel.snp_right)
      make.right.equalTo(self).offset(counterRightOffset)
    }
  }

}

获取头部的代码是:

  let mutualFriendsSectionView = ProfilePeopleListHeaderViewCell(title: Strings.Title.MutualFriends, numberOfPeople: 0)

  override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
     if section == 1 {
        //mutualFriendsSectionView.layoutSubviews()
        return mutualFriendsSectionView
      }
  }

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 40
    }

我在部分标题中看到绿色背景。 但是我没有看到任何标签...

【问题讨论】:

  • 在 IB 中而不是在代码中设计这个单元会更好也更容易。
  • 如果你有代码版的解决方案。我很感兴趣。否则,我不是。谢谢。
  • 您是否在代码中输入了numberOfSectionsInTableView?
  • 是的。就像: func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 3 }
  • 您的mutualFriendsSectionView 没有通过代码或自动布局约束设置任何框架。这可能是您的标签未显示的原因,因为标签约束与其父视图相关,即mutualFriendsSectionView。

标签: ios swift uitableview autolayout uitableviewsectionheader


【解决方案1】:

试试这个 UITableView Section Header。

  1. 以编程方式创建标题视图

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView.init(frame: CGRectMake(0, 0, tblView.bounds.size.width, 50))
        let lblHeader = UILabel.init(frame: CGRectMake(15, 13, tableView.bounds.size.width - 10, 24))
        if section == 0 {
            lblHeader.text = "Image Settings"
        }
        else if section == 1 {
            lblHeader.text = "Personal Settings"
        }
        else {
            lblHeader.text = "Other Settings"
        }
        lblHeader.font = UIFont (name: "OpenSans-Semibold", size: 18)
        lblHeader.textColor = UIColor.blackColor()
        headerView.addSubview(lblHeader)
        headerView.backgroundColor = UIColor(colorLiteralRed: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 1.0)
        return headerView
    }
    
  2. HeaderView 的高度

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    

【讨论】:

  • 请问可以使用代码格式化功能吗?那我试试你的解决方案:)
  • 我现在可以看到部分标题,但我仍然不明白为什么我的 ProfilePeopleListHeaderViewCell 类不起作用...我看到的唯一区别是您使用的是 CGRectMake 而不是自动布局.. .
【解决方案2】:

检查是否连接了 tableView 委托

【讨论】:

    【解决方案3】:

    实际上它正在工作。 我的错误是相信在类中也调用了 didSet{} -> 事实并非如此。

    当我在 init() 中设置标题时,它没有设置标签文本。

    【讨论】:

      【解决方案4】:

      尝试在 viewForHeader 方法中实例化你的 headerView

      如下编辑你的代码

      override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
      {
      
          let mutualFriendsSectionView = ProfilePeopleListHeaderViewCell(title: Strings.Title.MutualFriends, numberOfPeople: 0)
          if section == 1 
          {
              //mutualFriendsSectionView.layoutSubviews()
              return mutualFriendsSectionView
          }
      }
      

      【讨论】:

      • 我得到了同样的结果。绿色背景。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      相关资源
      最近更新 更多