【问题标题】:UICollectionView not showing & delegate/datasource methods not being calledUICollectionView 未显示 & 未调用委托/数据源方法
【发布时间】:2014-09-09 01:19:06
【问题描述】:

我有我的数据源方法

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 10;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    LPDiaryCollectionViewCell *cell = (LPDiaryCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    cell.testLabel.text = @"test";
    return cell;

}

单元格已注册

[self.collectionView registerClass:[LPDiaryCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

Datasource、delegate 和 outlet 已连接

但是当我运行时,不会调用数据源方法,并且视图是纯白色的,没有 UICollectionView 显示。

编辑:改为以编程方式添加 uicollectionview。它显示但仍然没有单元格。

【问题讨论】:

  • 这可能是一个愚蠢的问题,但数据源方法是在 Diary View Controller 中定义的吗?
  • 你在哪里制作的单元格?除非您完全用代码(没有 xib 或情节提要)制作单元格,否则不应注册单元格类。
  • xcode 没有调用以上三种方法...?
  • @danh 是的。单元格是以编程方式制作的 - 没有 xib,只是 collectionview 的故事板,而不是单元格。日志没有显示他们被调用
  • 你是否在IB中设置了“cell”作为cell标识符?

标签: ios uicollectionview


【解决方案1】:

您必须在故事板中为集合视图单元格设置单元格标识符(例如,我已声明 identifier = "ReuseCell")并在 cellForItemAtIndexPath 方法中使用相同的故事板标识符。

static NSString *identifier = @"ReuseCell";        
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

【讨论】:

    【解决方案2】:

    静态 NSString *identifier = @"Yourcellname";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    【讨论】:

      猜你喜欢
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      • 2011-08-12
      相关资源
      最近更新 更多