2015-10-26 19 views
7

Bir iOS uygulamasını hızlı bir şekilde yapıyorum ve bir koleksiyonView programlı olarak yapmaya çalışıyorum. UICollectionReusableView ürününün kendi alt sınıfını koleksiyon görünümü için bir başlık olarak kullanmak istiyorum çünkü bazı düğmelere ve üstbilgiye yapışan bir görüntüye ihtiyacım var.iOS; özel üstbilgilerle program aracılığıyla toplamaView

SupView, UICollectionReusableView'dir. Böyle, viewForSupplementaryElementOfKind yılında Tamamlayıcı Görünüm eklemeye çalışıyorum ama başlık oluşturmak çalışırken bir hata alıyorum

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! 
} 

hata let headerView = ... olduğunu ve diyor ki: "sinyal SIGABRT"

Headerview'ı nasıl başlatmalıyım, böylece akışımıma giriş yapabilirim? belki somewith

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

ile ama hata bana verir SupView-sınıfı kaydetmek çalışırsanız:

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

Herhangi Fikirler?

DÜZENLEME: alt sınıf uygulanması talep edildi

:

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'in sınıf uygulamasını göster – Shoaib

cevap

7

Bu yüzden, Mohamad Farhand'den ilham alarak anladım.

sorun yerine UICollectionReusableView.self veya alt sınıf örneğinin, bu CollectionView'ın ile canlarından alt sınıfı kayıt vardı .. Yani bu sorunumu çözdü: nasıl başlatmak için

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

Ve görünüm:

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

bunu şöyle yapabiliriz: AYRICA

// 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

Bu cevabı biraz daha derinleştirmeniz gerektiğini düşünüyorum. Benim durumumda “CollectionCustomHeader.self” ve “CustomeHeaderHeader” nedir? Ve iki tanımlayıcı eşleşecek mi? – Wiingaard

İlgili konular