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 に答える