在我的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];
}
答案 0 :(得分:1)
在您的课程中添加此方法并进行更改直至您的需要。
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 10.0f;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 10.0f;
}
答案 1 :(得分:1)
似乎没有设置要添加到集合视图单元格的子视图。问题出在这里:
// 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
,但是您没有在要添加的子视图中添加任何类型的约束。它的外观如下:
// 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
之类的东西,那么约束部分看起来会更简单。
让我知道它是否适合你
答案 2 :(得分:-1)
你必须使用UICollectionView的委托方法, 你必须为每个Cell提及你的itemSize。
像这样创建一个UICollectionViewFlowLayout:
UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc] init];
layOut.itemSize = CGSizeMake(cellWidth, cellHeight);
layOut.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layOut.minimumInteritemSpacing = 0;
layOut.minimumLineSpacing = 0;
yourCollectionViewObject.collectionViewLayout = layOut;
并使用此委托方法:
-(CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize itemSize = CGSizeMake(cellWidth, cellHeight);
return itemSize;
}