0

Ich erstelle eine UICollectionView programmgesteuert.UICollectionViewCell wird nicht angezeigt

Als Haftungsausschluss, folgte ich dieses Tutorial: http://randexdev.com/2014/07/uicollectionview/

So ist mein Setup wie folgt:

ich ein Storyboard mit einem UIScrollView haben, die durch ViewControllerA gesteuert wird.

In einer separaten Klasse CustomCollectionViewController Ich habe den folgenden Code:

import Foundation 
import UIKit 

class CustomCollectionViewController : UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { 

     let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
     layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10) 
     layout.itemSize = CGSize(width: 90, height: 120) 

     super.init(collectionViewLayout: layout) 

     collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
     //collectionView = UICollectionView(frame: CGRectMake(0, 0, 500, 500), collectionViewLayout: layout) 
     collectionView!.dataSource = self 
     collectionView!.delegate = self 
     collectionView!.backgroundColor = UIColor.redColor() 
     collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 
     self.view.addSubview(collectionView!) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 14 
    } 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) 
     cell.backgroundColor = UIColor.greenColor() 
     return cell 
    } 
} 

In meinem ViewControllerA ich diesen Code habe

let collectionViewController: CustomCollectionViewController = CustomCollectionViewController(nibName: nil, bundle: nil) 
collectionViewController.view.frame.origin = CGPointMake(scrollViewWidth*3, 0) 

scrollView.addSubview(collectionViewController.view) 

Wenn im Simulator läuft, kann ich den roten Hintergrund von dem sehen UICollectionView, aber überhaupt keine Zellen.

Irgendwelche Hinweise, was ich falsch mache?

+0

gesetzt, so können Sie einen Haltepunkt in Ihrer Collection gesetzt: cellForItemAtIndexPath: Funktion und sehen, ob es aufgerufen wird? – dirkgroten

+0

@dirkgroten habe ich schon gemacht. Es wird gerufen. – Phytochrome

Antwort

0

Das Problem war, dass ich nicht die ViewController als childViewController im viewControllerA

self.addChildViewController(collectionViewController) 
collectionViewController.didMoveToParentViewController(self) 
1

Möglicherweise -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath fehlt!

+0

Ich habe gerade versucht und hinzugefügt 'func collectionView (collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {return CGSizeMake (90, 120)}', aber es hat nichts geändert. – Phytochrome

+0

Ihr View-Controller ist eine Unterklasse von UICollectionViewController, versuchen Sie, sie in UIViewController zu ändern ODER versuchen Sie es mit self.collectionView = yourCollectionView – iOS77

+0

Nein. Keine Änderung wie auch immer. – Phytochrome