标头没有显示在UICollectionViewController Swift中

时间:2016-04-14 02:06:33

标签: ios xcode swift uicollectionview

我正在尝试向我的UICollectionViewController添加标头。我通过故事板完成了这一切。当我运行应用程序时,它不显示。

这就是我想在我的标题中显示的内容:

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "Header", forIndexPath: indexPath) as! ProfileCollectionReusableView

    header.editProfileButton.layer.cornerRadius = 5
    header.nameLabel.adjustsFontSizeToFitWidth = true
    header.nameLabel.text = PFUser.currentUser()!["name"] as? String

    header.profilePictureImageView.layer.cornerRadius = header.profilePictureImageView.frame.size.height/2
    header.profilePictureImageView.layer.masksToBounds = true

    header.profilePictureImageView.layer.borderColor = UIColor.whiteColor().CGColor

    return header

}

我没有在控制台上看到任何错误,也没有应用程序崩溃。只有一个白色屏幕,导航栏标题已更新为用户名。

5 个答案:

答案 0 :(得分:7)

如果使用.xib

let nib1:UINib = UINib(nibName: "ScheduleHeaderView", bundle: nil)

self.myCollection.register(nib1, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "ScheduleHeaderView")

设置标题的大小

let layout = self.myCollection.collectionViewLayout as! UICollectionViewFlowLayout
layout.headerReferenceSize = CGSize(width: 50, height: 180)

UICollectionViewDataSource

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

    let header:ScheduleHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "ScheduleHeaderView", for: indexPath) as! ScheduleHeaderView

    return header
}

答案 1 :(得分:1)

以下是您需要注意的一些提示:

1.实现UICollectionViewDelegateFlowLayout,如:

class MyCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {}

2.register标题类在使用之前:

self.collectionView!.registerClass(HeaderClass.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")

在方法ViewDidLoad();

3.设置UICollectionViewDelegateFlowLayout中的大小,如UITableViewDelegate

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSizeMake(Config.PHONE_WIDTH, 55)
}

4.像你已经完成的那样回复你自己的标题:

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
}

答案 2 :(得分:0)

问题很可能与标题部分的高度有关。我遇到了类似的问题,我正在注册标题但是没有设置高度。

docopt

//设置高度有助于它在页面上显示

let layout = UICollectionViewFlowLayout()
    layout.itemSize = CGSize(width: 20, height: 20)
    layout.minimumInteritemSpacing = 10
    layout.minimumLineSpacing = 10
    let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
    collectionView.backgroundColor = UIColor.white //default background color is black
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
    collectionView.register(Header.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCell")

答案 3 :(得分:0)

要显示标题,只需确保您的代码中的代码为数字“ 1”,如下所示:

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

答案 4 :(得分:-1)

嗨,请尝试以下代码

//MARK:- COLLECTION VIEW
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
    return yourArr.count
}

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

    switch kind {
    case UICollectionElementKindSectionHeader:
        let headerView = homeCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HomeHeaderCell", for: indexPath) as! HomeHeaderCell
        headerView.UpdateHeaderCell(indexPath: indexPath)
        return headerView
    case UICollectionElementKindSectionFooter:
        let footerView = homeCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HomeFooterCell", for: indexPath) as! HomeFooterCell
        return footerView
    default:
        return UICollectionReusableView()
    }
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: collectionView.frame.width, height: 80)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
    return CGSize(width: collectionView.frame.width, height: 10)
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
    let myCell:HomeCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionViewCell", for: indexPath) as! HomeCollectionViewCell
        myCell.backgroundColor = UIColor.clear
        myCell.layer.borderWidth = 1
        myCell.layer.borderColor = UIColor.clear.cgColor
        myCell.layer.cornerRadius = 10
        myCell.UpdateCell()
        return myCell as HomeCollectionViewCell;
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
     print("User tapped on \(indexPath.row)")
}

//Use for size
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {

    if DeviceType.IS_IPAD{
            return CGSize(width: ((self.view.frame.width-80)/4) , height: 120);
        }
        return CGSize(width: ((self.view.frame.width-80)/3) , height: 120);
}

//Use for interspacing
func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 10.0
}

func collectionView(_ collectionView: UICollectionView, layout
    collectionViewLayout: UICollectionViewLayout,
                    minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 10.0
}