【问题标题】:Displaying uicollectinviewcellls using subclass of UICollectionViewFlowLayout使用 UICollectionViewFlowLayout 的子类显示 uicollectinviewcellls
【发布时间】:2012-10-15 06:35:32
【问题描述】:

我正在尝试通过子类化 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

【问题讨论】:

    标签: ios uicollectionview


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 1970-01-01
      • 2018-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多