UICollectionView:重装数据调用numberOfItemsInSection但不调用cellForItemAtIndexPath

时间:2014-12-07 03:50:08

标签: ios objective-c uicollectionview

我有一个简单的UICollectionView。第一次加载视图时,将调用cellForItemAtIndexPath三次,我看到三个单元格。

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
    NSUInteger count = ([self.results count] > 0 ? [self.results count] : 3);
    NSLog(@"count %lu", (unsigned long)count);
    return count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cellForItemAtIndexPath called");
    UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"TasteCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

然而,后来的self.results变成了一个包含5个元素的数组,我称之为:

[self.collectionView reloadData];

当发生这种情况时,再次调用numberOfItemsInSection并且那里的“count”日志语句显示它返回5.但是,这次,从不调用cellForItemAtIndexPath,并且UI仍然显示第一次加载时的三个单元格。

为什么即使计数已更改,单元格数也未更新?

3 个答案:

答案 0 :(得分:1)

尝试再次为UICollectionView设置数据源和委托,然后重新加载它。

-(void)reloadCollectionView {
  mycollectionView.dataSource = self;
  mycollectionview.delegate = self;
  [mycollectionview reloadData];
}

答案 1 :(得分:1)

我想,可能是你正在使用静态集合视图单元的概念。 如果是静态的,请在xib / Storyboard中将其更改为动态。

答案 2 :(得分:0)

请检查您是否已将collectionView的数据源和委托连接到视图控制器。 如果不这样做

        -(void)viewDidLoad
          {
              [super viewDidLoad];
               mycollectionView.dataSource = self;
               mycollectionview.delegate = self;
          }

希望这会对你有所帮助。

相关问题