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.
(Wo) nennt man bootstrapBars() in der Viewcontroller? – Mischa
Tut mir leid, es ist in einer 'boostrapPageOne()' Methode gemacht. Ich werde den Code aktualisieren, um es ein wenig klarer zu machen. –