在我的水平 CollectionView 中,没有正确显示 View 。为什么?
下图中粉红色的是CollectionView,橙色的是cell,为什么cell显示的很奇怪。对于单元格 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];
}
最佳答案
您添加到 Collection View 单元格的 subview 似乎没有被设置。问题出在这里:
// 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;
}
您只是调用 addSubview
但您没有在要添加的 subview 上添加任何类型的约束。它应该是这样的:
// 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];
UIView *albumsView = [[[NSBundle mainBundle] loadNibNamed:@"AlbumsView" owner:self options:nil] objectAtIndex:0]
[cell.PagerViewCell addSubview: albumsView];
// Adding constraints:
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(albumsView);
[cell.PagerViewCell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[albumsView]|" options:0 metrics:nil views:viewsDictionary]];
[cell.PagerViewCell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[albumsView]|" options:0 metrics:nil views: viewsDictionary]];
return cell;
}
约束使您的 albumView
填充单元格。据我了解,这是预期的效果。
如果您使用类似 PureLayout
的东西,约束部分看起来会简单得多。
让我知道它是否适合你
关于ios - UICollectionView 单元格显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45236741/