swift 4无法在我的UIcollectionView页面上显示标题

时间:2019-03-15 15:30:00

标签: ios swift uicollectionview

我正在尝试创建一个类似于instagram的个人资料页面,但是无法在我的UICollectionView(UserProfileVC.swift)页面上显示标题(UserProfileHeader.swift)。

我试图通过在每个代码块中添加打印语句来调试代码,如下面的附加快照所示,当我进入模拟器上的配置文件页面时,唯一打印到控制台中的代码是numberOfSections和numberOfItemsInSection函数,我不知道这是应该发生的还是其他函数没有被访问。我在这里做什么错了?

import UIKit
import Firebase

private let reuseIdentifier = "Cell"
private let headerIdentifier = "UserProfileHeader"

class UserProfileVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    // MARK: Properties

    let db = Firestore.firestore() // Connects firestore

    let customGrayColor = UIColor(red: 247/255, green: 247/255, blue: 242/255, alpha: 1)

    override func viewDidLoad() {
        super.viewDidLoad()

        let settings = db.settings
        settings.areTimestampsInSnapshotsEnabled = true
        db.settings = settings

        // Register cell classes
        self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        self.collectionView!.register(UserProfileHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerIdentifier)


        // Background color
        self.collectionView?.backgroundColor = customGrayColor

        fetchCurrentUserData()
    }


    // MARK: UICollectionView

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        print("numberOfSections works -------------------->")
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        print("numberOfItemsInSection -------------------->")
        return 0
    }


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        print("referenceSizeForHeaderInSection -------------------->")

        return CGSize(width: view.frame.width, height: 200)
    }


    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        // Declare header
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) as! UserProfileHeader
        print("dequeueReusableSupplementaryView -------------------->")

        // Return header
        return header
    }


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

        // Configure the cell
        print("dequeueReusableCell -------------------->")
        return cell
        }
}

UserProfileHeader.swift脚本

import UIKit

class UserProfileHeader: UICollectionViewCell {

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

        self.backgroundColor = .red

        print("UserProfileHeader -------------------->")


    }

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

控制台

    nw_protocol_boringssl_get_output_frames(1301) [C1.1:2][0x7faa496143b0] get output frames failed, state 8196
2019-03-15 09:59:29.118574-0500 HobbiStyle[35259:1722282] [BoringSSL] 
    nw_protocol_boringssl_get_output_frames(1301) [C1.1:2][0x7faa496143b0] get output frames failed, state 8196
2019-03-15 09:59:29.119123-0500 HobbiStyle[35259:1722282] TIC Read Status [1:0x0]: 1:57
2019-03-15 09:59:29.119274-0500 HobbiStyle[35259:1722282] TIC Read Status [1:0x0]: 1:57
numberOfSections works -------------------->
numberOfItemsInSection -------------------->

1 个答案:

答案 0 :(得分:0)

我发现了问题,两个脚本都没问题,问题出在我的标签栏控制器中,我将profileVC声明为UICollectionViewLayout而不是UICollectionViewFlowLayout。归功于@StephanDowless