【问题标题】:Separate UICollectionView dataSource and delegate not being called不调用单独的 UICollectionView 数据源和委托
【发布时间】: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


【解决方案1】:

foobarhandler 具有作为委托的弱引用。所以当你离开bootstrapBars的范围时,它就被释放了。

将其存储在本地以保留参考:

var myDelegate : FooBarCollectionHandler?

func bootstrapBars() {
    //your stuff
    myDelegate = FooBarCollectionHandler()
    barCollectionView.delegate = myDelegate
    barCollectionView.dataSource = myDelegate

}

【讨论】:

  • 漂亮,对我有用!您是否参考了说明委托是弱引用的文档?
  • 是的:developer.apple.com/library/ios/documentation/UIKit/Reference/… 仅供参考,所有的委托都必须是弱变量,否则你将有保留周期,然后内存泄漏
  • 太棒了,我很欣赏这个链接。我也会牢记weak 的要求。
猜你喜欢
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
  • 2015-04-11
  • 2016-03-03
相关资源
最近更新 更多