2016-06-01 9 views
3

Ich versuche, eine separate Klasse für meine UICollectionView DataSource und Delegate zu verwenden, aber die Methoden werden nicht aufgerufen.Separate UICollectionView dataSource und Delegat nicht aufgerufen

Die Hierarchie Ansicht ist wie folgt:

(Root VC)  -> (Current Screen) -> (Current Page)  -> (CollectionView in question) 
UIViewController -> UIViewController -> UIPageViewController -> UICollectionView 

Dies ist die Klasse für die Current Screen VC.

import UIKit 

class FooController: UIViewController { 
    // PageViewController 
    let pageController = UIPageViewController(/*...*/) 
    let pageOne = UIViewController() 
    let pageTwo = UIViewController() 

    override func loadView() { 
    super.loadView() 
    // Setup Stuff 
    bootstrapPageOne() 
    } 

    func bootstrapPageOne() { 
    // Do PageView Stuff 
    bootstrapBars() 
    } 

    func bootstrapBars() { 
    // Everything is set up when this function is called. 

    let collectionViewFlowLayout = UICollectionViewFlowLayout() 
    let cellSize : CGFloat = 60 

    let collectionViewWidth = view.bounds.width - 20 * 2 //CGFloat(barsCount.count) * cellSize 
    let collectionViewHeight = cellSize 
    let collectionViewFrame : CGRect = CGRect(x: 20, y: view.bounds.height - (60 + view.layer.cornerRadius), width: collectionViewWidth, height: collectionViewHeight) 

    let barCollectionView = UICollectionView(frame: collectionViewFrame, collectionViewLayout: collectionViewFlowLayout) 
    let fooBarHandler = FooBarCollectionHandler() 
    barCollectionView.delegate = fooBarHandler 
    barCollectionView.dataSource = fooBarHandler 
    barCollectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "barCellIdentifier") 
    barCollectionView.backgroundColor = UIColor.blueColor() 

    pageOne.view.addSubview(barCollectionView) 
    } 
} 

Dies ist die Klasse für die Datasource Collection ist und Delegierter

import UIKit 

class FooBarCollectionHandler: NSObject, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource { 
    let settings = SettingsHandler.sharedSettings 

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    debugPrint(#function) 
    return 1 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    debugPrint(#function) 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("barCellIdentifier", forIndexPath: indexPath) 

    return cell 
    } 

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    debugPrint(#function) 
    return settings.bars.count // 5 
    } 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    debugPrint(#function) 
    return CGSizeMake(40,40) 
    } 
} 

// MARK: - Touch handling 
extension FooBarCollectionHandler { 
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    debugPrint(#function) 
    debugPrint(indexPath.row) 
    } 
} 

Mit der aktuellen Konfiguration keine der Delegierten/Datenquelle Methoden aufgerufen werden. Ich kann barCollectionView.numberOfSections() oder barCollectionView.numberOfItemsInSection(_:) anrufen und es wird anrufen und zurückgeben, aber sie werden nicht automatisch aufgerufen.

HINWEIS: Die UICollectionView macht gut, aber die Zellen nicht.

+0

(Wo) nennt man bootstrapBars() in der Viewcontroller? – Mischa

+0

Tut mir leid, es ist in einer 'boostrapPageOne()' Methode gemacht. Ich werde den Code aktualisieren, um es ein wenig klarer zu machen. –

Antwort

9

Die foobarhandler hat eine schwache Referenz als Delegate. Wenn Sie also den Bereich bootstrapBars verlassen, wird er freigegeben.

speichern Sie es lokal halten einen Verweis:

var myDelegate : FooBarCollectionHandler? 

func bootstrapBars() { 
    //your stuff 
    myDelegate = FooBarCollectionHandler() 
    barCollectionView.delegate = myDelegate 
    barCollectionView.dataSource = myDelegate 

} 
+0

Schön, das hat bei mir funktioniert! Haben Sie einen Verweis auf die Dokumentation, die besagt, dass die Delegierten schwache Referenzen sind? –

+1

Ja: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/#//apple_ref/occ/instp/UICollectionView/delegate FYI, alle Delegierten müssen schwach var sein, sonst Sie werden Zyklen behalten und dann Speicherlecks – CZ54

+1

Fantastisch, ich schätze den Link. Ich werde auch die "schwache" Anforderung berücksichtigen. –

Verwandte Themen