2015-02-26 5 views
27

मैं कई दिनों की कोशिश की इस साकार करने के लिए: enter image description hereएकाधिक collectionView - आईओएस तेज

मैं अपने UIViewController दो अलग CollectionView में जोड़ना चाहते हैं। उदाहरण के लिए मैं इन संग्रहों में छवियां रखना चाहता हूं दृश्य प्रत्येक संग्रह दृश्य अपनी छवियों का उपयोग करता है। क्या यह संभव है?

अगर कोई मुझे हाथ दे सकता है तो मैं बहुत खुश हूं। :)

उत्तर

71

यह वह जगह है संभव है, आपको बस प्रत्येक UICollectionView को सबव्यूव के रूप में जोड़ने की आवश्यकता है, और प्रतिनिधि और डेटा स्रोत को अपने UIViewController पर सेट करें।

यहां एक त्वरित उदाहरण है।

let collectionViewA = UICollectionView() 
let collectionViewB = UICollectionView() 
let collectionViewAIdentifier = "CollectionViewACell" 
let collectionViewBIdentifier = "CollectionViewBCell" 

override func viewDidLoad() { 
    // Initialize the collection views, set the desired frames 
    collectionViewA.delegate = self 
    collectionViewB.delegate = self 

    collectionViewA.dataSource = self 
    collectionViewB.dataSource = self 

    self.view.addSubview(collectionViewA) 
    self.view.addSubview(collectionViewB) 
} 

cellForItemAtIndexPath प्रतिनिधि समारोह में:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    if collectionView == self.collectionViewA { 
     let cellA = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewAIdentifier) as UICollectionViewCell 

     // Set up cell 
     return cellA 
    } 

    else { 
     let cellB = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewBIdentifier) as UICollectionViewCell 

     // ...Set up cell 

     return cellB 
    } 
} 

numberOfItemsInSection समारोह में मान लें कि आप एक UICollectionView काम कर रहे है, तो आप काफी आसानी से एक दूसरे को जोड़ने के लिए अपने स्वयं के उपयोग के लिए इस कोड को अनुकूलित करने के लिए सक्षम होना चाहिए :

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    if collectionView == self.collectionViewA { 
     return 0 // Replace with count of your data for collectionViewA 
    } 

    return 0 // Replace with count of your data for collectionViewB 
} 
+0

मुझे यह त्रुटि मिलती है "UICollectionView को इस लाइन पर एक गैर-शून्य लेआउट पैरामीटर के साथ प्रारंभ किया जाना चाहिए" संग्रह ViewA = UICollectionView() " – dennis

+1

यह मेरे लिए काम नहीं किया गया है! मुझे यह त्रुटि मिली: *** अपरिपक्व अपवाद के कारण ऐप को समाप्त करना 'एनएसआईएनवालिडअर्गमेंट एक्सेप्शन', कारण: 'यूआईसीओलेक्शन व्यू को गैर-शून्य लेआउट पैरामीटर के साथ आरंभ किया जाना चाहिए' –

5

हाँ - यह पूरी तरह से संभव है। आप अलग अलग वर्गों के लिए अपने-अपने UICollectionViewDelegates/UICollectionViewDataSources असाइन कर सकते हैं या तो या CollectionViews उपवर्ग, अपने वर्तमान ViewController करने के लिए दोनों प्रतिनिधि और डेटा स्रोत बताए और इतने तरह प्रतिनिधिमंडल तरीकों में collectionView करने के लिए अपने संदर्भ खिन्न:

@IBOutlet collectionViewA: CustomCollectionViewA! 
@IBOutlet collectionViewB: CustomCollectionViewB! 


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    if let a = collectionView as? CustomCollectionViewA { 
     return a.dequeueReusableCellWithIdentifier("reuseIdentifierA", forIndexPath: indexPath) 
    } else { 
     return collectionView.dequeueReusableCellWithIdentifier("reuseIdentifierB", forIndexPath: indexPath)  
    } 
} 
+0

"संग्रह दृश्यों को उपclass" से आपका क्या मतलब है? मैं जो कह रहा हूं वह करने के लिए मैं प्रबंधन नहीं करता था। – Damasio

1

इसी collectionviews के लिए दुकानों बनाएँ: दुकानों:

+०१२३५१६४१०
@IBOutlet weak var collectionView: UICollectionView! 

@IBOutlet weak var SecondCollectioView: UICollectionView! 

विधि:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as UICollectionViewCell 

    if(collectionView == self.SecondCollectioView) { 
     cell.backgroundColor = UIColor.black 
    } else { 
     cell.backgroundColor = self.randomColor() 
    } 

    return cell; 
} 

यह एक एक और तरीका हो जाएगा।

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