HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 单独的UICollectionView数据源和委托未被调用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试为我的UICollectionView Datasource和Delegate使用单独的类,但是没有调用这些方法. @H_419_7@

@H_419_7@视图层次结构如下:

@H_419_7@

(Root VC)        -> (Current Screen) -> (Current PagE)       -> (CollectionView in question)
UIViewController -> UIViewController -> UIPageViewController -> UICollectionView
@H_419_7@这是Current Screen VC的类.

@H_419_7@

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)
  }
}
@H_419_7@这是CollectionView Datasource和Delegate的类

@H_419_7@

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)
  }
}
@H_419_7@使用当前配置,不会调用任何委托/数据源方法.我可以调用barCollectionView.numberOfSections()或barCollectionView.numberOfItemsInSection(_ :),它将调用并返回,但它们不会自动调用.

@H_419_7@注意:UICollectionView呈现正常,但单元格没有.

解决方法

foob​​arhandler有一个弱参作为代表.因此,当您离开bootstrapBars的范围时,它将被释放. @H_419_7@

@H_419_7@将其存储在本地以保留参

@H_419_7@

var myDelegate : FooBarCollectionHandler?

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

}

大佬总结

以上是大佬教程为你收集整理的ios – 单独的UICollectionView数据源和委托未被调用全部内容,希望文章能够帮你解决ios – 单独的UICollectionView数据源和委托未被调用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。