Tay*_*ser 3 iphone ios uicollectionview swift
我正在尝试为我的UICollectionViewDataSource和Delegate 使用单独的类,但是没有调用这些方法.
视图层次结构如下:
(Root VC) -> (Current Screen) -> (Current Page) -> (CollectionView in question)
UIViewController -> UIViewController -> UIPageViewController -> UICollectionView
Run Code Online (Sandbox Code Playgroud)
这是Current ScreenVC 的类.
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)
}
}
Run Code Online (Sandbox Code Playgroud)
这是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)
}
}
Run Code Online (Sandbox Code Playgroud)
使用当前配置,不会调用任何委托/数据源方法.我可以打电话barCollectionView.numberOfSections()或者barCollectionView.numberOfItemsInSection(_:)它会调用并返回,但它们不会自动调用.
注意:UICollectionView呈现正常,但单元格没有.
CZ5*_*Z54 11
在foobarhandler具有弱引用作为代表.所以当你离开范围时bootstrapBars,它就被释放了.
将其存储在本地以保留参考:
var myDelegate : FooBarCollectionHandler?
func bootstrapBars() {
//your stuff
myDelegate = FooBarCollectionHandler()
barCollectionView.delegate = myDelegate
barCollectionView.dataSource = myDelegate
}
Run Code Online (Sandbox Code Playgroud)