简体   繁体   English

UICollectionView隐藏我的补充视图,为什么?

[英]UICollectionView hiding my supplementary view, why?

So I'm creating a UISegmentedControl , which I am putting in a supplementary view for a collection view. 因此,我正在创建一个UISegmentedControl ,将其放入集合视图的补充视图中。 I am then using that segmented control, through a delegate on it's IBAction , to set a BOOL type property on my UICollectionViewController , which appropriately changes the data being sent out from one stream to another. 然后,我通过IBAction上的委托使用该分段控件,以在我的UICollectionViewController上设置BOOL type属性,该属性适当地将从一个流发送到另一个流的数据更改。 This all seems to work, as when I tap on the second tab after loading the collection view (which loads with the first tab selected, and the right data for the first tab), the right data shows up, but my supplementary view goes away! 这一切似乎都奏效,因为当我在加载集合视图后加载第二个选项卡(加载时选择了第一个选项卡,并且第一个选项卡具有正确的数据)时,显示了正确的数据,但是我的辅助视图消失了!

What's odd is this: 奇怪的是:

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

This method is properly being called, and I am properly setting the size of my supplementary view, and I can see so in my simulator, since my data is offset by the size of my supplementary view. 正确地调用了此方法,并且我正确设置了辅助视图的大小,并且可以在模拟器中看到,因为我的数据被辅助视图的大小所抵消。 What's NOT being called, though, is this method: 但是,没有被调用的是这个方法:

 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 

This method is only called when my collection view is loaded, thus loading the initial segmented controller, but is never called again, even though I am calling reloadData on the collectionView and the size method mentioned above is called again. 仅在加载我的集合视图时才调用此方法,从而加载了初始的分段控制器,但是即使我在collectionView上调用reloadData并且再次调用了上述size方法,也再也不会调用此方法。

Now to add another twist, I went through and put a breakpoint in one of the methods for reloading the collection view, and did: 现在要增加另一种扭曲,我经历了一个断点,在其中一种重新加载集合视图的方法中进行了操作:

po [self.view recursiveDescription]

In the debugger, to find out if my segmented controller view is still there, and it is! 在调试器中,找出我的分段控制器视图是否仍然存在! It has it's hidden flag set to YES, though... 它的隐藏标志设置为YES,但是...

baseClass = UICollectionReusableView; frame = (0 0; 320 55); hidden = YES; layer = <CALayer: 0xef4bd30>

How do I get this view to not be hidden, when the only reference I have to it is in the method called: 当我唯一需要的引用是在称为方法的方法中时,如何不隐藏该视图:

 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

and this method isn't even being called this second time around? 甚至第二次都没有调用此方法?

So the answer has to do with FlowLayouts, and I wasn't properly setting mine up. 因此答案与FlowLayouts有关,而我的设置不正确。 I was indeed getting a size for my supplementary header, but I wasn't giving it attributes in the method: 我的确确实为补充标头设置了一个大小,但是我没有在方法中赋予它属性:

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

Which is only called when you add a supplementary view to the attributes array in: 仅当在以下情况下向属性数组添加补充视图时才调用该方法:

- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect 

Once I added an attribute for supplementary view of kind UICollectionElementKindSectionHeader, and the layout method was being called, then part of my troubles were abated. 一旦我为UICollectionElementKindSectionHeader类型的补充视图添加了一个属性,并且调用了layout方法,那么部分麻烦就减轻了。

Another problem was that I was checking index paths for sticky cells that I was trying to get to stick to the top of my collection view, and this index path checking collided with my supplementary view index path and was giving me trouble. 另一个问题是我正在检查要粘贴到集合视图顶部的粘性单元格的索引路径,并且此索引路径检查与我的补充视图索引路径相冲突,给我带来麻烦。

So to summarize: Make sure you are using the UICollectionViewFlowLayout methods! 总结一下:确保您正在使用UICollectionViewFlowLayout方法! And make sure you don't have colliding index paths that remove your attributes from the attributes array. 并确保没有冲突的索引路径从属性数组中删除属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM