UICollectionView自定义单元格布局

时间:2015-09-18 15:37:13

标签: ios layout uicollectionview uicollectionviewcell

我有一个UICollectionView,我尝试自定义它。

这就是我想要的: enter image description here

这是我的所作所为: enter image description here

我认为从UICollectionViewDelegate实现sizeForItemAtIndexPath就足够了。

这是我正在做的事情: - 我返回屏幕宽度的一半,用于所有单元格的宽度 - 对于第一个和第三个单元格,我返回50的高度 - 对于第二个单元格,我返回100的高度。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

使用简单的UICollectionViewDelegateFlowLayout方法无法做到这一点。你有UICollectionViewFlowLayout的子类。

非常脏示例如何执行此操作

@interface CVFlowLayout1 : UICollectionViewFlowLayout

@property (nonatomic, strong) NSDictionary *layoutInfo;

@end

@implementation CVFlowLayout1

- (instancetype)init
{
    if (self = [super init])
    {
        self.minimumLineSpacing = 0;
        self.minimumInteritemSpacing = 0;
    }
    return self;
}

- (void)prepareLayout
{
    NSMutableDictionary *newLayoutInfo = [NSMutableDictionary dictionary];
    NSMutableDictionary *cellLayoutInfo = [NSMutableDictionary dictionary];

    NSInteger sectionCount = [self.collectionView numberOfSections];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];

    for (NSInteger section = 0; section < sectionCount; section++) {
        NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];

        for (NSInteger item = 0; item < itemCount; item++) {
            indexPath = [NSIndexPath indexPathForItem:item inSection:section];

            UICollectionViewLayoutAttributes *itemAttributes =
            [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
            itemAttributes.frame = CGRectMake(item == 1 ? [UIScreen mainScreen].bounds.size.width/2.0 : 0, section == 0 ? 0 : 50, [UIScreen mainScreen].bounds.size.width/2.0, item == 1 ? 100 : 50);

            cellLayoutInfo[indexPath] = itemAttributes;
        }
    }

    newLayoutInfo[@"Cell"] = cellLayoutInfo;

    self.layoutInfo = newLayoutInfo;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return self.layoutInfo[@"Cell"][indexPath];
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray *allAttributes = [NSMutableArray arrayWithCapacity:self.layoutInfo.count];

    [self.layoutInfo enumerateKeysAndObjectsUsingBlock:^(NSString *elementIdentifier,
                                                         NSDictionary *elementsInfo,
                                                         BOOL *stop) {
        [elementsInfo enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath,
                                                          UICollectionViewLayoutAttributes *attributes,
                                                          BOOL *innerStop) {
            if (CGRectIntersectsRect(rect, attributes.frame)) {
                [allAttributes addObject:attributes];
            }
        }];
    }];

    return allAttributes;
}

@end

只需将该类的实例设置为collectionView.flowLayout属性

即可
CVFlowLayout1 *layout = [[CVFlowLayout1 alloc] init];
[_collectionView setCollectionViewLayout:layout];
相关问题