我有一个UICollectionView
彼此重叠的单元格。
我将它们设置zIndex
在一个UICollectionViewLayout
子类中,UICollectionViewLayoutAttributes
以便它们在视觉上按所需顺序显示。轻触单元格的重叠区域时zIndex
,通过该collectionView:didSelectItemAtIndexPath:
方法,触摸平整的击中区域将按预期的方式与较高的单元格一起工作。
我过渡到一个UICollectionViewFlowLayout
子类,当我过渡到第一UICollectionViewLayout
个子类zIndex
时,点击重叠区域时并不总是选择具有较高的单元格。
进一步的调查显示,子视图中UICollectionViewCell
s的顺序UICollectionView
正在确定要点击哪个单元格。
我已经编写了一种重新排序子视图的方法,但这似乎有点不合常规。
是否有更好的方法让UICollectionView
单元格collectionView:didSelectItemAtIndexPath:
根据单元格的外观返回预期的索引路径?
根据我的测试,似乎布局到布局的转换会忽略zIndex
推送视图控制器的布局属性。这闻起来像个臭虫。您可以通过在视图控制器出现后重新加载来解决此问题:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Reloading will clear selection, so we remember which index paths are selected before reload
NSArray *selectedIndexPaths = self.collectionView.indexPathsForSelectedItems;
NSIndexSet *allSections = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.collectionView.numberOfSections)];
[self.collectionView performBatchUpdates:^{
// First, reload to correct the zOrder
[self.collectionView reloadSections:allSections];
// Then, re-select the index paths
if (selectedIndexPaths.count)
{
for (NSIndexPath *indexPath in selectedIndexPaths)
{
[self.collectionView selectItemAtIndexPath:indexPath
animated:YES
scrollPosition:UICollectionViewScrollPositionNone];
}
}
} completion:nil];
}
仍然有点麻烦,但是比手动重新排序集合视图的子视图要少一些麻烦。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句