2016-04-14 16 views
2

UICollectionViewControllerにヘッダーを追加しようとしています。私はストーリーボードを通してこれをすべて行った。私はアプリを実行すると、表示されません。私は、コンソール上の任意のエラーを見ていないですUICollectionViewControllerでヘッダーが表示されないSwift

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 

} 

もアプリのクラッシュを行います。

これは私が私のヘッダーに表示しようとしていますものです。ナビゲーションバーのタイトルがユーザーのユーザー名に更新された白い画面が表示されます。あなたが使用する前に

class MyCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {} 

2.registerヘッダークラ​​ス:

1.Implement UICollectionViewDelegateFlowLayoutのような:ここで

+0

に表示して、それを助けましたか? – ozgur

+0

私はそうではないと私はそれが何であるか分からない、これは私の最初のヘッダを追加しようとしています、ありがとう! – m1234

+0

アトリビュートインスペクタでレイアウトが流れている場合は – m1234

答えて

1

はあなたが何をすべきかに注意を払う必要があるいくつかのヒントがあります

self.collectionView!.registerClass(HeaderClass.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header") 
メソッド内のViewDidLoad();

3.SETあなたはまだ行って何のような独自のヘッダ4.return UITableViewDelegate

ようUICollectionViewDelegateFlowLayoutでサイズ
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
    return CGSizeMake(Config.PHONE_WIDTH, 55) 
} 

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

これは私にとってはうまくいくもので、あなたにとってはうまくいくと思います – Benjamin

4

あれば使用

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

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

.xib

ヘッダーのセットサイズ

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 
} 
+0

ありがとうございます。 – RaziPour1993

0

問題は非常によくヘッダ部の高さとすることができます。私はヘッダーを登録していたが、高さが設定されていない同様の問題に遭遇しました。

高さを設定
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") 

//あなたはフローレイアウトの権利を使用しているページ

layout.headerReferenceSize = CGSize(width: 50, height: 180) 
関連する問題