我正在尝试为我的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
渲染正常,但单元格没有。
答案 0 :(得分:11)
foobarhandler
有一个弱引用作为委托。因此,当您离开bootstrapBars
的范围时,它将被释放。
将其存储在本地以保留参考:
var myDelegate : FooBarCollectionHandler?
func bootstrapBars() {
//your stuff
myDelegate = FooBarCollectionHandler()
barCollectionView.delegate = myDelegate
barCollectionView.dataSource = myDelegate
}