使用UICollectionViewFlowLayout的子类显示uicollectinviewcellls

时间:2012-10-15 06:35:33

标签: ios uicollectionview

我试图通过继承UICollectionViewFlowLayout来在定义的点(存储在核心数据中)显示单元格。当我添加对象并且我已经在查看集合视图时,此代码在定义的点中显示单元格,但是在加载或刷新集合视图时,对象不会出现。我已经通过自定义UICollectionViewFlowLayout连接到self.collectionView.collectionViewLayout,但我唯一一次引用它是在cellforitematindexpath中,当我传递包含它的坐标的对象时。我错过了什么?

#import "DayViewLayout.h"

@interface DayViewLayout () {
    NSMutableArray *_insertedIndexPaths;
    NSMutableArray *_deletedIndexPaths;
}

@end

@implementation DayViewLayout


- (void)prepareLayout {
    [super prepareLayout];
    _insertedIndexPaths = [NSMutableArray new];
    _deletedIndexPaths = [NSMutableArray new];
}

- (void)prepareForCollectionViewUpdates:(NSArray*)updates
{
    [super prepareForCollectionViewUpdates:updates];
    for (UICollectionViewUpdateItem *updateItem in updates) {
        if (updateItem.updateAction == UICollectionUpdateActionInsert) {
            [_insertedIndexPaths addObject:updateItem.indexPathAfterUpdate];
        }
        else if (updateItem.updateAction == UICollectionUpdateActionDelete) {
            [_deletedIndexPaths addObject:updateItem.indexPathBeforeUpdate];
        }
    }
}


- (void)finalizeCollectionViewUpdates
{
    [_insertedIndexPaths removeAllObjects];
    [_deletedIndexPaths removeAllObjects];
}


- (UICollectionViewLayoutAttributes*)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath*)itemIndexPath
{
    if ([_insertedIndexPaths containsObject:itemIndexPath]) {

        UICollectionViewLayoutAttributes *attributes =
        [UICollectionViewLayoutAttributes
         layoutAttributesForCellWithIndexPath:itemIndexPath];


        CGRect visibleRect =
        (CGRect){.origin = self.collectionView.contentOffset,
            .size = self.collectionView.bounds.size};
        attributes.center = CGPointMake(CGRectGetMidX(visibleRect),
                                        CGRectGetMidY(visibleRect));
        attributes.alpha = 0.0f;
        attributes.transform3D = CATransform3DMakeScale(0.6f,
                                                        0.6f,
                                                        1.0f);


        return attributes;
    } else {
        return [super initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];
    }
}


- (UICollectionViewLayoutAttributes*)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath*)itemIndexPath
{
    if ([_deletedIndexPaths containsObject:itemIndexPath]) {
        UICollectionViewLayoutAttributes *attributes =
        [UICollectionViewLayoutAttributes
         layoutAttributesForCellWithIndexPath:itemIndexPath];

        CGRect visibleRect =
        (CGRect){.origin = self.collectionView.contentOffset,
            .size = self.collectionView.bounds.size};
        attributes.center = CGPointMake(CGRectGetMidX(visibleRect),
                                        CGRectGetMidY(visibleRect));
        attributes.alpha = 0.0f;
        attributes.transform3D = CATransform3DMakeScale(1.3f,
                                                        1.3f,
                                                        1.0f);

        return attributes;
    } else {
        return [super finalLayoutAttributesForDisappearingItemAtIndexPath:itemIndexPath];
    }
}

-(UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath*)indexPath {
    UICollectionViewLayoutAttributes *attributes =
    [super layoutAttributesForItemAtIndexPath:indexPath];
    [self applySettingsToAttributes:attributes];
    return attributes;
}

- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
    // 1
    NSArray *layoutAttributes = [super layoutAttributesForElementsInRect:rect];
    [layoutAttributes enumerateObjectsUsingBlock:
     ^(UICollectionViewLayoutAttributes *attributes,
       NSUInteger idx, BOOL *stop)
     {
         [self applySettingsToAttributes:attributes];
     }];
    return layoutAttributes;
}

-(void)applySettingsToAttributes:(UICollectionViewLayoutAttributes*)attributes {
    // 1
    NSIndexPath *indexPath = attributes.indexPath;
    attributes.zIndex = -indexPath.item;

    // 2
    attributes.frame= CGRectMake([_object.x floatValue], [_object.y floatValue], [_object.width floatValue], [_object.height floatValue]);
}

@end

1 个答案:

答案 0 :(得分:0)

我解决这个问题的方法是在collectionviewcontroller加载来自fetchedresultscontroller的对象及其索引路径时创建一个字典。然后将此字典传递给UICollectionViewFlowLayout。在UICollectionViewFlowLayout中的applySettingsToAttributes方法中,我将对象从字典中取出,并从对象中设置属性框架。

我之前的问题是我在创建单元格时设置了对象,这只在插入新对象时才起作用,而不是在加载/刷新视图时。