[英]Separate UICollectionView dataSource and delegate not being called
我正在嘗試為我的UICollectionView
DataSource和Delegate使用單獨的類,但是沒有調用這些方法。
視圖層次結構如下:
(Root VC) -> (Current Screen) -> (Current Page) -> (CollectionView in question)
UIViewController -> UIViewController -> UIPageViewController -> UICollectionView
這是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)
}
}
這是CollectionView DataSource和Delegate的類
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)
}
}
使用當前配置,不會調用任何委托/數據源方法。 我可以調用barCollectionView.numberOfSections()
或barCollectionView.numberOfItemsInSection(_:)
,它將調用並返回,但它們不會自動調用。
注意: UICollectionView
呈現正常,但單元格沒有。
foobarhandler
有一個弱參考作為代表。 因此,當您離開bootstrapBars
的范圍時,它將被釋放。
將其存儲在本地以保留參考:
var myDelegate : FooBarCollectionHandler?
func bootstrapBars() {
//your stuff
myDelegate = FooBarCollectionHandler()
barCollectionView.delegate = myDelegate
barCollectionView.dataSource = myDelegate
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.