嵌套在 Collectionview 中的 CollectionView [英] CollectionView nested inside Collectionview

查看:28
本文介绍了嵌套在 Collectionview 中的 CollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过将集合视图嵌套在表视图中的解决方案,但对于我的应用程序,我需要有 2 个集合视图,因为这样可以更轻松地执行其他一些操作.

I have seen solutions where a collection view is nested inside a table view but for my app I need to have 2 collection views as it makes it easier to do some other things.

所以让我们调用仅垂直滚动的根集合视图 VerticalCollectionView 和仅水平滚动的嵌套集合视图 Horizo​​ntalCollectionView.我使用故事板创建了它们.下面你会看到橙色是垂直的,绿色的水平是里面有标签的.

So lets call the root collection view VerticalCollectionView which only scrolls vertically and the nested collection view HorizontalCollectionView which only scrolls horizontally. I created them using the Storyboard. Below you'll see the orange is the Vertical with the green Horizontal with a label inside it.

我已经设置了委托 &两个集合的数据源到同一个 CollectionViewController.

And I have set the delegate & datasource of both collections to the same CollectionViewController.

我通过检查委托方法引用的表视图来区分 2 个不同的单元格

I distinguish between the 2 different cells by checking which tableview the delegate method is referencing as such

我的问题是 Horizo​​ntalCollectionView 没有被实例化.我拥有适用于 VerticalCollectionView、背景颜色、项目数量等的所有内容.在 VerticalCollectionViewCell 中,我有一个 IBOutlet 引用 Horizo​​ntalCollectionView

My problem is that HorizontalCollectionView isn't getting instantiated. I have everything working for VerticalCollectionView, the background color, the number of items, etc. In the VerticalCollectionViewCell, I have an IBOutlet referencing HorizontalCollectionView

推荐答案

我在我最近的应用中经常使用嵌套的 collectionViews.起初它对我不起作用,但是当我知道它消失时它变得非常简单.

I have used nested collectionViews in my recent app a lot. at first it didn't work for me but when I learned its away it became pretty simple.

不是将 Horizo​​ntalCollectionView 委托和数据源设置为同一个 CollectionViewController,而是创建一个 UICollectionViewCell 并在单元格的 awakeFromNib() 函数中设置委托和数据源,并在该单元格中编写 Horizo​​ntalCollectionView 函数.它就像一个魅力;)

instead of setting the HorizontalCollectionView delegate and dataSource to the same CollectionViewController make a UICollectionViewCell and set the delegates and dataSource in awakeFromNib() function of the cell and write the HorizontalCollectionView functions in that cell. it works like a charm ;)

如果您需要更详细的答案,请告诉我.

if you needed more detailed answer let me know.

这篇关于嵌套在 Collectionview 中的 CollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆