我需要获取嵌套在另一个collectionView中的collectionView的引用,以便我可以执行UI更新。
在我的ViewController课程中,我有一个"外部"的CollectionView:
@IBOutlet var outerCollectionView: UICollectionView!
我为" inner"设置了DataSource和Delegate。 willDisplayCell中的collectionView:
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let outerViewCell = cell as? OuterCollectionCell else { return }
outerViewCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forItem: indexPath.item)
outerViewCell.innerCollectionView.reloadData()
outerViewCell.innerCollectionView.layoutIfNeeded()
}
然后我可以在cellForItemAt中填充两个collectionViews的单元格:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.outerCollectionView {
let outerCell: OuterCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierOuter, for: indexPath) as! OuterCollectionCell
outerCell.labelCell.text = self.packArray[indexPath.item].title
// other stuff....
return outerCell
} else {// innerCollectionView
let innerCell: InnerCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierInner, for: indexPath) as! InnerCollectionCell
innerCell.imageCell.file = self.multiPartArray[collectionView.tag][indexPath.item].image
// other stuff....
return innerCell
}
}
这会产生以下结果:
其中collectionView.tag是" inner"的实例。 collectionView(0为蓝色等)。
现在我想对单元格执行UI更新,并且由于各种原因无法使用cellForItemAt。所以我以前做过的事情就像:
if let editCell = collectionView.cellForItem(at: indexPath) as? InnerCollectionCell {
editCell.imageCell.image = UIImage(named: "test")
}
但要做到这一点,我需要参考"内部"我没有拥有的collectionView,因为它可能在执行这些UI更新时被重用。但我所拥有的是collectionView.tag,它只是一个Int引用,以及我想要执行更新的单元格的indexPath。
我可以在collectionView中指定的单元格的UI,我没有直接引用或者这只是一厢情愿的想法吗?
我应该使用某种代理功能,我只能想到如何。下面的参考也是我的OuterCollection类,我定义了"内部" collectionView并设置数据源和委托。
class OuterCollectionCell: UICollectionViewCell {
@IBOutlet var labelCell: UILabel!
// define the smaller collection view that is in the cell
@IBOutlet var innerCollectionView: UICollectionView!
// set delegate and datasource for new collectionView
// this is download collection manager
func setCollectionViewDataSourceDelegate
<D: UICollectionViewDataSource & UICollectionViewDelegate>
(dataSourceDelegate: D, forItem item: Int) {
innerCollectionView.delegate = dataSourceDelegate
innerCollectionView.dataSource = dataSourceDelegate
innerCollectionView.tag = item
innerCollectionView.reloadData()
innerCollectionView.layoutIfNeeded()
}
}
答案 0 :(得分:1)
您可以在OuterCollectionCell类中定义innercollectionview的委托和数据源方法。试试这个: -
// Set the delegate and datasource in your custom cell class as below:-
class OuterCollectionCell: UITableViewCell {
// set the delegate & datasource of innercollectionView in the init or
// awakefromnib or storyboard.
// I have set it in storyboard and it works for me.
// Implement the datasource and delegate methods of innercollectionView in this class.
}
这样,innerCollectionView的委托和数据源将与outercollectionview分开,您可以在OuterCollection单元类中处理innecrCollectionView的所有逻辑。