2015-10-26 14 views
7

के साथ देखें मैं तेजी से एक आईओएस ऐप बना रहा हूं, और मैं प्रोग्राम बनाने की कोशिश कर रहा हूं प्रोग्रामेटिक रूप से देखें। मैं संग्रह दृश्य के लिए शीर्षलेख के रूप में UICollectionReusableView के अपने उप-वर्ग का उपयोग करना चाहता हूं, क्योंकि मुझे हेडर में कुछ बटन और एक स्ट्रेच करने योग्य छवि चाहिए।आईओएस; प्रोग्रामेटिक रूप से संग्रह कस्टम हेडर

सुपरव्यू UICollectionReusableView है।

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

मैं इस तरह, viewForSupplementaryElementOfKind में पूरक देखें सम्मिलित करने के लिए कोशिश कर रहा हूँ, लेकिन मैं जब हेडर बनाने के लिए प्रयास में कोई त्रुटि हो रही है:

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 = ... में है और कहते हैं: "सिग्नल SIGABRT"

मुझे हेडर्व्यू को कैसे प्रारंभ करना चाहिए, इसलिए मैं अपने फ्लोलेआउट में इनपुट कर सकता हूं? शायद

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)'

किसी भी विचार?

संपादित करें:

उपवर्ग के कार्यान्वयन अनुरोध किया गया था:

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

तो मैंने मोहम्मद फराहण्ड से प्रेरणा के साथ इसे समझ लिया।

समस्या यह है कि मैं उपवर्ग यह collectionView साथ खुद को पंजीकृत करने के लिए, UICollectionReusableView.self या उपवर्ग के कहने के बजाय था .. तो यह मेरी समस्या हल:

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

और कैसे प्रारंभ करने में दृश्य:

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

आप इस तरह यह कर सकते हैं:

// 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' क्या है? और क्या दो पहचानकर्ता मिलान करना चाहते हैं? – Wiingaard

संबंधित मुद्दे