演示我的问题的最佳方式是观看下面的视频:
http://foffer.dk/collectionview.mp4
如您所见,在我开始滚动之前,我的 collectionView 不会更新其布局以正确显示项目。我正在使用单独的 collectionViewFlowLayout 模型。这是我的代码:
CollectionViewFlowLayout.m:
@implementation CollectionViewFlowLayout
- (void)prepareLayout {
CGFloat halfWidth = self.collectionView.bounds.size.width / 2;
CGFloat halfHeight = self.collectionView.bounds.size.height / 2;
self.itemSize = CGSizeMake(halfWidth, halfHeight);
self.minimumInteritemSpacing = 0;
self.minimumLineSpacing = 0;
}
// indicate that we want to redraw as we scroll
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}
@end
我的 PhotosCollectionViewController(我省略了标准的 collectionView 内容,因为我认为它对这个问题不重要,如果重要请告诉我,我会发布它):
-(instancetype)init {
UICollectionViewFlowLayout *layout = [[CollectionViewFlowLayout alloc] init];
return (self = [super initWithCollectionViewLayout:layout]);
}
-(void) viewDidLoad{
[super viewDidLoad];
[self.collectionView registerClass:[FOFPhotoCell class] forCellWithReuseIdentifier:@"photo"];
self.collectionView.backgroundColor = [UIColor whiteColor];
CGFloat bottomLayoutGuide = self.tabBarController.tabBar.frame.size.height;
self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, bottomLayoutGuide, 0);
}
-(BOOL)shouldAutorotate{
return YES;
}
我需要在某处调用 invalidateLayout 吗?我似乎无法弄清楚在哪里实现该方法。 [self.collectionView invalidateLayout]
不起作用。
有人能让我走上正轨吗?
提前致谢 克里斯
最佳答案
所以,事实证明我把它设置错了。作为this article来自苹果 说:
Before you start building custom layouts, consider whether doing so is really necessary.
The UICollectionViewFlowLayout class provides a significant amount of behavior that has already been optimized for efficiency and that can be adapted in several ways to achieve many different types of standard layouts. The only times to consider implementing a custom layout are in the following situations:
The layout you want looks nothing like a grid or a line-based breaking layout (a layout in which items are placed into a row until it’s full, then continue on to the next line until all items are placed) or necessitates scrolling in more than one direction.
You want to change all of the cell positions frequently enough that it would be more work to modify the existing flow layout than to create a custom layout.
而且我的布局配置非常简单。所以我把它复杂化了。
关于ios - CollectionView 布局在滚动之前不会刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23163036/