0

我试图通过子类化 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
4

1 回答 1

0

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

我之前的问题是我在创建单元格时设置了对象,这仅在我插入新对象时有效,而在加载/刷新视图时无效。

于 2012-10-15T19:12:01.623 回答