【发布时间】:2016-06-01 14:14:47
【问题描述】:
我正在尝试为我的 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 数据源和委托的类
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 可以正常渲染,但单元格不能。
【问题讨论】:
-
(在哪里)你在 ViewController 中调用 bootstrapBars() 吗?
-
抱歉,这是在
boostrapPageOne()方法中完成的。我将更新代码以使其更清晰。
标签: ios iphone swift uicollectionview