【问题标题】:How to add UICollectionView Header如何添加 UICollectionView 标头
【发布时间】:2018-10-10 12:38:45
【问题描述】:

我希望以编程方式向我的 UICollectionView 添加一个标签,并使用 viewForSupplementaryElementOfKind 和 referenceSizeForHeaderInSection 来设置它,但是由于某种原因,当我设置我的视图时,它仍然将它放在我的 CollectionView 的第一行而不是创建的标题。 As you can see in this screenshot, "Today" is in the first cell, instead of the header I created for it

class TvController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

private let cellId = "cellId"
private let headerId = "headerId"

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "TV"
    navigationController?.navigationBar.isTranslucent = false

    collectionView?.backgroundColor = .white

    collectionView?.register(TvCell.self, forCellWithReuseIdentifier: cellId)
    collectionView?.register(Header.self, forCellWithReuseIdentifier: headerId)

}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)

    return cell
}

//Row for each TV show
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: 120)
}

//Today's date header
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let header = collectionView.dequeueReusableCell(withReuseIdentifier: headerId, for: indexPath) as! Header

    return header
}

//Today's date header
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: view.frame.width, height: 60)
}
}

这里是 Header 类

class Header: UICollectionViewCell  {

override init(frame: CGRect)    {
    super.init(frame: frame)
    setupHeaderViews()
}

let dateLabel: UILabel = {
    let title = UILabel()
    title.text = "Today"
    title.textColor = .gray
    title.backgroundColor = .black
    title.font = UIFont(name: "Montserrat", size: 17)
    title.translatesAutoresizingMaskIntoConstraints = false
    return title
}()

func setupHeaderViews()   {
    addSubview(dateLabel)

    dateLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: 20).isActive = true
    dateLabel.topAnchor.constraint(equalTo: topAnchor, constant: 10).isActive = true
    dateLabel.widthAnchor.constraint(equalToConstant: 120).isActive = true
    dateLabel.heightAnchor.constraint(equalToConstant: 30).isActive = true
}


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

【问题讨论】:

    标签: ios iphone swift


    【解决方案1】:

    您使用错误的方法来注册您的标头。 您还使用错误的方法出列。由于您将标头注册为 forSupplementaryViewOfKind - 您必须使用 'dequeueReusableSupplementaryView' 方法而不是 'dequeueReusableCell'

    来使用 deque 标头
     override func viewDidLoad() {
        collectionView?.register(Header.self, forSupplementaryViewOfKind:
           UICollectionElementKindSectionHeader, withReuseIdentifier: headerId)
      }
    
    
    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind:
        String, at indexPath: IndexPath) -> UICollectionReusableView {
            let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier:
                headerId, for: indexPath) as! Header
            return header
    }
    

    【讨论】:

    • 感谢 antwash 的回答,现在看来我在构建中遇到了错误。它指出:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使一种视图出列:带有标识符 headerId 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或类或连接故事板中的原型单元格”你知道我现在必须做什么吗?
    • 查看编辑,它显示了您应该在 viewDidLoad 和 viewForSupplementaryElementOfKind 方法中拥有的内容。希望你明白:)
    • 啊,我明白了,对此感到抱歉,我还在学习 :) 非常感谢您的帮助!我现在明白了。
    • 别担心,我已经 5 个月进入 ios 并且每天都在学习更多 =]
    【解决方案2】:
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderId", for: indexPath) as! HeaderClassname
        switch kind {
        case UICollectionElementKindSectionHeader:
            if indexPath.section == 0 {
                //Your Code Here
            } else {
            }
            return headerView
    
        default:
            assert(false, "Unexpected element kind")
        }
    
        return headerView
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      相关资源
      最近更新 更多