2015-10-26 13 views
7

iOSアプリケーションを迅速に作成しています。コレクションビューをプログラムで作成しようとしています。 UICollectionReusableViewの独自のサブクラスをcollectionviewのヘッダーとして使用したいのですが、ヘッダーにはいくつかのボタンと伸縮可能なイメージが必要です。iOS;プログラムでコレクションを作成するカスタムヘッダーで表示する

SupViewはUICollectionReusableViewです。私はこのように、viewForSupplementaryElementOfKindに補足ビューを挿入しようとしているが、私は、ヘッダーを作成しようと、エラーを取得しています

override func viewDidLoad() { 
    super.viewDidLoad() 


    let layout = UICollectionViewFlowLayout() 
    layout.headerReferenceSize = CGSizeMake(self.view.frame.width, 200) 

    someView = SupView(frame: CGRectMake(0, 0, view.frame.width, 200)) 

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
    collectionView.delegate = self 
    collectionView.dataSource = self 

    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 
    collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell") // UICollectionReusableView 
    self.view.addSubview(collectionView) 
} 

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { 
    var reusableView : UICollectionReusableView? = nil 

    // Create header 
    if (kind == UICollectionElementKindSectionHeader) { 
     // Create Header 
     let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell", forIndexPath: indexPath) as! SupView 
     headerView.frame = CGRectMake(0, 0, view.frame.width, 200) 

     reusableView = headerView 
    } 
    return reusableView! 
} 

エラーがlet headerView = ...であると述べています: "signal SIGABRT"

私はflowlayoutに入力できるように、ヘッダービューをどのように初期化する必要がありますか?は多分

collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell") 

でsomewithが、私はSupViewクラスを登録しようとした場合、それは私にエラーを与える:

.../collectionViewPlay/ViewController.swift:32:24: Cannot invoke 'registerClass' with an argument list of type '(SupView!, forSupplementaryViewOfKind: String, withReuseIdentifier: String)'

任意のアイデア?

EDIT:サブクラスの実装が要求された

import UIKit 

    class SupView: UICollectionReusableView { 

    ////////////////////////////////////////////////////////////////////////////// 
    override init(frame: CGRect) { 
     super.init(frame: frame) 
     self.myCustomInit() 
    } 


    ////////////////////////////////////////////////////////////////////////////// 
    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder)! 
     self.myCustomInit() 
    } 

    func myCustomInit() { 
     print("hello there from SupView") 
    } 

} 
+0

SupViewのクラス実装を表示 – Shoaib

答えて

7

モハマド・ファーハンドのインスピレーションを得てわかりました。

問題は、私が代わりにUICollectionReusableView.selfまたはサブクラスのインスタンスの、それはcollectionViewで自己サブクラスを登録しなければならなかったということでした...これは私の問題を解決しました:

collectionView.registerClass(SupView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader , withReuseIdentifier: "someRandonIdentifierString") 

そして、どのように初期化するために、表示:

someView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "someRandonIdentifierString", forIndexPath: indexPath) as! SupView 
1

は、あなたがこのようにそれを行うことができます。ALSO

// Setup Header 
self.collectionView?.registerClass(CollectionCustomHeader.self, forSupplementaryViewOfKind: CustomeHeaderHeader, withReuseIdentifier: "customHeader") 

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

    if kind == CustomeHeaderHeader { 
     let view = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "parallaxHeader", forIndexPath: indexPath) 
     return view 
    } 
+1

私はあなたがその答えを少し深める必要があると思います。私の場合、 'CollectionCustomHeader.self'と' CustomeHeaderHeader'は何ですか? 2つの識別子は一致すると考えられますか? – Wiingaard

関連する問題