2012-10-15 20 views
0

我試圖通過繼承UICollectionViewFlowLayout子類來顯示定義點(存儲在覈心數據中)的單元格。當我添加一個對象並且我正在查看collectionview時,此代碼顯示定義點中的單元格,但是當集合視圖加載或刷新時,對象不會顯示。我通過自定義UICollectionViewFlowLayout連接到self.collectionView.collectionViewLayout,但唯一一次我引用它是在cellforitematindexpath,當我通過它包含它的座標的對象。我錯過了什麼?使用UICollectionViewFlowLayout的子類顯示uicollectinviewcellls

#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 

回答

0

我解決這個問題的方法是在collectionviewcontroller從fetchedresultscontroller加載對象及其索引路徑時創建一個字典。這個字典然後被傳遞給UICollectionViewFlowLayout。在UICollectionViewFlowLayout的applySettingsToAttributes方法中,我將對象從字典中取出並設置對象的屬性框架。

我以前的問題是,我正在創建單元格時設置對象,只有當我插入一個新的對象,而不是當視圖被加載/刷新時才起作用。