简体   繁体   English

UICollectionView自定义单元格布局

[英]UICollectionView custom cell layout

I have a UICollectionView and I'm trying customize it a little. 我有一个UICollectionView,我正在尝试自定义它。

Here's what I want: 这就是我想要的: 在此输入图像描述

Here's what I have: 这就是我所拥有的: 在此输入图像描述

I thought implementing sizeForItemAtIndexPath from UICollectionViewDelegate would be enough. 我认为从UICollectionViewDelegate实现sizeForItemAtIndexPath就足够了。

Here's what I'm doing: - I returning half of the screen's width for all the cell's widths - For the first and third cells I'm returning a height of 50 - For the second cell I'm returning a height of 100. 这就是我正在做的事情: - 我返回屏幕宽度的一半,用于所有单元格的宽度 - 对于第一个和第三个单元格,我返回的高度为50 - 对于第二个单元格,我返回的高度为100。

What am I doing wrong? 我究竟做错了什么?

There is no way to do that with simple UICollectionViewDelegateFlowLayout methods. 使用简单的UICollectionViewDelegateFlowLayout方法无法做到这UICollectionViewDelegateFlowLayout You have subclass from UICollectionViewFlowLayout . 你有UICollectionViewFlowLayout子类。

Here is very dirty example how you can do this 这是一个非常脏的例子,你怎么能这样做

@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

just set instance of that class to your collectionView.flowLayout property 只需将该类的实例设置为collectionView.flowLayout属性即可

CVFlowLayout1 *layout = [[CVFlowLayout1 alloc] init];
[_collectionView setCollectionViewLayout:layout];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM