ios - UICollectionView 单元格显示不正确

标签 ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

在我的水平 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];
}

它的显示是这样的 Cell Image 1

和单元格 2 Cell Image 2

和单元格 3 Cell Image 3

最佳答案

您添加到 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/

相关文章:

objective-c - 如何围绕任意点旋转对象?

ios - 是否可以直接在 XCode 8 中创建 .xcarchive?

ios - 使用 iTunesConnect TestFlight 添加测试人员

ios - 如何使用 Multipack Texture Atlas 加载 CCSpriteBatchNode

iphone - 检查缩放级别是否改变

objective-c - 为什么 nilObject isEqual nilObject 返回 no?

objective-c - 调试帮助书 ("The selected topic is currently unavailable")

swift - UICollectionView ScrollTo 崩溃

ios - 在 UICollectionViewCell 的 subview 上调用 removeFromSuperview 在第一次加载时不起作用,仅当单元格被重用时

ios - CollectionView 单元格意外行为 swift