首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionViewFlowLayout未正确换行

UICollectionViewFlowLayout未正确换行
EN

Stack Overflow用户
提问于 2014-01-08 04:35:15
回答 2查看 1.5K关注 0票数 2

我已经将flow layout子类化,并在我的集合视图上将其设置为我的collectionViewLayout

然后我将布局设置如下:

代码语言:javascript
复制
- (id) init
{
    if(self = [super init])
    {
        self.minimumInteritemSpacing = 11.0;
        self.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.itemSize = CGSizeMake(64.0,32.0);
        self.minimumLineSpacing = 10.0;
        self.sectionInset = UIEdgeInsetsMake(11.0,95.0,11.0,11.0);

        return self;
    }
    return nil;
}

95 + 64 + 11 + 64 + 11 + 64 + 11 = 320 -我检查过了。(这是一个左侧插图,3个单元格,2个空格和一个右侧插图)

我有一个部分,72个项目,我的索引路径函数的单元格:

代码语言:javascript
复制
- (UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifierForCustomCell forIndexPath:indexPath];

    cell.mainLabel.text = [@(indexPath.item) stringValue];

    return cell;
}

输出是这样的:

如您所见,每行只有2个单元格。此外,它不是每隔3个单元格显示一次。

我看了一下flow layout的布局函数产生了什么

代码语言:javascript
复制
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {

给予..。

这是layoutAttributes [NSArray]中的前3个布局属性,正如您所看到的,它决定只将前2项和第4项视为矩形可见(320 X 568)。这一定是集合视图布局中的错误,不是吗?集合视图充满整个屏幕,宽度为320。因此,由于这是一个换行布局,任何项目都不应该离开屏幕。

删除边缘插图后:

它的工作方式就像它应该的那样。但我想有节插图,因为我需要添加一些装饰视图,这需要在相同的滚动视图,而我需要一个更大的左插图比右插图。

EN

回答 2

Stack Overflow用户

发布于 2014-01-08 04:45:32

苹果的UICollectionViewFlowLayout中有一个错误,一些单元格的显示是越界的。这些资源将帮助您修复它(您必须创建UICollectionViewFlowLayout的子类并覆盖此方法):

代码语言:javascript
复制
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
    NSMutableArray *newAttributes = [NSMutableArray arrayWithCapacity:attributes.count];
    for (UICollectionViewLayoutAttributes *attribute in attributes) {
        if ((attribute.representedElementCategory != UICollectionElementCategoryCell) || // always include everything that's not a cell
            ((attribute.frame.origin.x + attribute.frame.size.width <= (self.collectionViewContentSize.width - self.sectionInset.right)) &&
             (attribute.frame.origin.y + attribute.frame.size.height <= (self.collectionViewContentSize.height - self.sectionInset.bottom))))
        {
            [newAttributes addObject:attribute];
        }
    }
    return newAttributes;
}

此修复的基础来源:

票数 1
EN

Stack Overflow用户

发布于 2014-01-08 05:45:29

在有人弄清楚发生了什么以及这是否是一个bug之前,我目前用来修复这个问题的方法是:

代码语言:javascript
复制
- (NSArray *)alterAttributes:(NSArray *)attributes
{
    for (UICollectionViewLayoutAttributes *attribute in attributes) {

        switch (attribute.indexPath.item % 3) {
            case 0:
                attribute.center = CGPointMake(CellX1,attribute.center.y);
                break;
            case 1:
                attribute.center = CGPointMake(CellX2,attribute.center.y);
                break;
            case 2:
                attribute.center = CGPointMake(CellX3,attribute.center.y);
                break;

            default:
                break;
        }
    }
    return attributes;
}

手动更改layoutAttributesForElementsInRect中的x坐标--这并不是一个令人满意的解决方案。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20981345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档