未在CoreData Change上调用UICollectionView DataSource方法

时间:2013-10-16 17:14:57

标签: ios core-data uicollectionview nsfetchedresultscontroller uicollectionreusableview

我有一个UICollectionView,其内容由NSFetchedResultsController提供。每当更新核心数据时,都会调用通过reloadItemsAtIndexPaths:方法更新集合视图中更改的项目。

经过大量测试后,我确定上述过程正常。我对核心数据进行了更新,最终调用了数据源方法“cellForItemAtIndexPath”,并按预期更新了单元格。但是,我也实施了 数据源方法'viewForSupplementaryElementOfKind',显示一些单元格标题(也基于对核心数据的更改),但这不能正常工作。

出于某种原因,似乎当核心数据发生变化后调用'reloadItemsAtIndexPaths'时,'viewForSupplementaryElementOfKind'没有被调用,我无法弄清楚为什么会出现这种情况。但是,一旦我开始滚动集合视图, 然后调用'viewForSupplementaryElementOfKind',我能够根据核心数据的变化看到补充标题视图中的更新。在初始创建UICollectionView时,也会成功调用此方法。

我有一个自定义布局,我正在使用我的集合视图,所以问题可能就在那里?我希望有人能发现我的错误。

以下是用于创建UICollectionView,其布局,单元格及其标题视图的代码:

  1. 创建UICollectionView

            - (void)createCollectionView
            {
                _layout1 = [[BigLayout alloc] init]; //custom layout
                [_layout1 setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
                _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout1];
                _collectionView.translatesAutoresizingMaskIntoConstraints = NO;
                _collectionView.backgroundColor = [UIColor clearColor];
    
                _collectionView.dataSource = self;
                _collectionView.delegate = self;
    
                [self.view addSubview:_collectionView];
    
                //set up constraints, add them to view...
    
                [self.collectionView registerClass:[InboxCell class]
                        forCellWithReuseIdentifier:@"inbox"];
                [self.collectionView registerClass:[UICollectionReusableView class]
                        forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                               withReuseIdentifier:@"header"];
            }
    
  2. 创建自定义布局

        @implementation BigLayout
    
        -(id)init
        {
            self = [super init];
            if (self) {
                self.itemSize = CGSizeMake(330, 588);
                self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
                self.headerReferenceSize = CGSizeMake(13, 13);
            }
            return self;
        }
    
        -(void)prepareLayout
        {
            [super prepareLayout];
    
            _cellCount = [[self collectionView] numberOfItemsInSection:0];
        }
    
        - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path
        {
            UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:path];
            attributes.size = self.itemSize;
            attributes.center = CGPointMake(path.item * (self.itemSize.width + 20) + self.itemSize.width/2.0 + 20, self.collectionView.center.y);
            return attributes;
        }
    
        - (CGSize)collectionViewContentSize
        {
            return CGSizeMake(((self.itemSize.width + 20) * _cellCount) + 80, [self collectionView].height);
        }
    
        -(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
        {
            NSMutableArray* attributes = [NSMutableArray array];
            for (NSInteger i=0 ; i < self.cellCount; i++) {
                NSIndexPath* indexPath = [NSIndexPath indexPathForItem:i inSection:0];
    
                UICollectionViewLayoutAttributes *attr = [self layoutAttributesForItemAtIndexPath:indexPath];
                UICollectionViewLayoutAttributes *hattr = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
    
                if (CGRectIntersectsRect(attr.frame, rect)) {
                    [attributes addObject:attr];
                    [attributes addObject:hattr];
                }
            }
            return attributes;
        }
    
        - (void)prepareForCollectionViewUpdates:(NSArray *)updateItems
        {
    
        }
    
        - (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
        {
            UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
            attributes.alpha = 1.0;
            return attributes;
        }
    
        - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
        {
            UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:indexPath];
            attributes.size = CGSizeMake(13, 13);
            attributes.center = CGPointMake(indexPath.item * (self.itemSize.width + 20) + self.itemSize.width/2.0 + 20 + self.collectionView.left, self.collectionView.height == 768 ? 75 : 200);
            attributes.alpha = 1.0f;
            return attributes;
        }
    
  3. 单元格的数据源方法(在CD更新时调用)

            - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
            {
                //this method gets called on reload items
                InboxCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"inbox" forIndexPath:indexPath];
                Email *email = [self.fetchedResultsController objectAtIndexPath:indexPath];
                //do stuff with the email in the cell
                return cell;
            }
    
  4. 补充标题视图的数据源方法(未在CD更新时调用)

            - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
                       viewForSupplementaryElementOfKind:(NSString *)kind
                                             atIndexPath:(NSIndexPath *)indexPath
            {
                //this method does not get called on reload items, only gets called initially and on scroll
                UICollectionReusableView *reusableView;
    
                 if (kind == UICollectionElementKindSectionHeader) { //specify in case we add a footer later
    
                     UICollectionReusableView *unreadEmailImageIdentifier = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
                     Email *email = [self.fetchedResultsController objectAtIndexPath:indexPath];
                     UIImageView *unreadEmailDot = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dot.png"]];
                     [unreadEmailImageIdentifier addSubview:unreadEmailDot];
                     unreadEmailImageIdentifier.hidden = YES;
    
                     if (email.isUnread == YES || email.isUnread == 1) {
                         unreadEmailImageIdentifier.hidden = NO;
                     }
                     reusableView = unreadEmailImageIdentifier;
                 }
    
                return reusableView;
            }
    
  5. 简而言之,当我尝试重新加载集合视图或集合视图的组件时,似乎不会调用补充标题视图的数据源方法。但是,我确实看到了为自定义布局类IS中的补充视图创建属性的方法。如果有人能指出任何可能的原因,那可能是 发生了,我很感激!

1 个答案:

答案 0 :(得分:0)

reloadItemsAtIndexPaths仅用于重新加载单元格。您可以使用reloadSections:重新加载该部分来重新加载补充视图。如果您仍然遇到问题,请在问题的NSFetchedResultsControllerDelegate实施中加入。