选择子单元格

时间:2017-02-12 22:57:12

标签: ios swift delegates uicollectionview datasource

我试图在collectionView中设置一个colletionView,在数据源之间分离,这样我就可以更容易地操作UI元素了。

我有我的主ViewController,它设置“外部”集合视图的数据源和委托。

class DownloadCollectionController: UIViewController {

@IBOutlet weak var outerCollectionView: UICollectionView!
var outerDataProvider : OuterDatasourceDelegate!
var outerDelegate: OuterDatasourceDelegate!

override func viewDidLoad() {
    super.viewDidLoad()

    // set the datasource fot the outer collection
    outerDataProvider = OuterDatasourceDelegate()

    // crashes on this line when selecting a cell
    outerCollectionView.dataSource = outerDataProvider

    outerDelegate = OuterDatasourceDelegate()
    outerCollectionView.delegate = outerDelegate
}
}

OuterDatasourceDelegate类有一些函数来填充单元格,然后是willDisplayAt函数,它为“内部”集合视图设置数据源和委托:

class OuterDatasourceDelegate: NSObject, UICollectionViewDataSource, UICollectionViewDelegate {


func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return GlobalOperationStore.sharedInstance.globalPackArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    // cell stuff...
    return outerCell
}


func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

    // set the datasource and delegate for the inner collection
    guard let outerViewCell = cell as? OuterCollectionCell else { return }
    let dataProvider = InnerDatasourceDelegate()
    let delegate = InnerDatasourceDelegate()
    outerViewCell.initializeCollectionViewWithDataSource(dataProvider, delegate: delegate, forItem: indexPath.item)
    outerViewCell.innerCollectionView.reloadData()
    outerViewCell.innerCollectionView.layoutIfNeeded()
}

}

此InnerDataSourceDelegate类与上面填充单元格所需的功能相同。

我的自定义单元类引用了“inner”collectionView并包含上面调用的initializeCollectionViewWithDataSource函数:

class OuterCollectionCell: UICollectionViewCell {

var collectionViewDataSource : UICollectionViewDataSource!
var collectionViewDelegate : UICollectionViewDelegate!


// define the smaller collection view that is in the cell
@IBOutlet var innerCollectionView: UICollectionView!

// set delegate and datasource for new collectionView
func initializeCollectionViewWithDataSource<D: UICollectionViewDataSource, E: UICollectionViewDelegate>(_ dataSource: D, delegate :E, forItem item: Int) {

    self.collectionViewDataSource = dataSource
    self.collectionViewDelegate = delegate

    innerCollectionView.delegate = self.collectionViewDelegate
    innerCollectionView.dataSource = self.collectionViewDataSource
    innerCollectionView.tag = item


    innerCollectionView.reloadData()
    innerCollectionView.layoutIfNeeded()

}

首次加载时显示正常:

enter image description here

但是当我按照我的要求选择一个单元格时打印:

cell no: 0 of collection view: 2

然后我得到一个致命的错误:

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)

在此行的ViewController类中:

outerCollectionView.dataSource = outerDataProvider

因为outerCollectionView是零。但我无法弄清楚为什么它是零。我不知道为什么它再次通过viewDidLoad,因为当我离开VC并返回时,ViewController现在不会再次加载?

-----编辑------

0 个答案:

没有答案