【发布时间】:2017-12-27 10:49:47
【问题描述】:
在我水平的 CollectionView 中,无法正确显示视图。为什么?
在下面显示的图片中,粉色是 CollectionView,橙色是单元格,为什么单元格显示奇怪。对于单元格 1,有右侧空间,对于单元格 2,有左侧空间,对于单元格 3,左侧的大小增加了一点。为什么会有这种奇怪的行为?
这是我的代码:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 3;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell* cell = (CollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"pagerCell" forIndexPath:indexPath];
[cell.PagerViewCell addSubview: [[[NSBundle mainBundle] loadNibNamed:@"AlbumsView" owner:self options:nil] objectAtIndex:0]];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(pagerView.frame.size.width , pagerView.frame.size.height);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 1;
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[pagerView.collectionViewLayout invalidateLayout];
}
【问题讨论】:
-
您是否为您的 collectionview 创建了 UICollectionViewFlowLayout ??
标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout