UICollectionView单元格是不可见的

时间:2012-11-06 16:06:14

标签: uicollectionviewcell

我最近在我的故事板上添加了一个UICollectionView,它目前被另一个视图推入视图,这似乎工作正常但是,使用故事板编辑器我设置视图包含35个单元格,在编辑器中看起来很好,但是我运行应用程序,细胞是不可见的。一些单元格内部有UIButton,这些单元格也不会渲染。

要仔细检查视图是否渲染我在编辑器中更改了背景颜色并再次运行,颜色正确更新。我是否正确地假设设置应该自动呈现单元格而不必在编辑器中设置任何委托代码?

任何帮助都将不胜感激。

由于

EDIT ----

好的,我已经在我的第二个视图控制器中实现了相应的委托方法,然后推送segue来调出collectionview控制器,我还将其添加到我的第二个视图控制器头文件中,并将以下代码添加到.M文件中:

// number of sections for the view
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

// numbers of items in section
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1;
}

// cell to return (uses the custom identifier of the cell in the storyboard)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"quickNoteCell" forIndexPath:indexPath];
    return cell;
}

我还确保故事板上单元格中的标识符使用quickNoteCell,并将其默认颜色更改为蓝色,但是我仍然没有看到该单元格的任何想法?

4 个答案:

答案 0 :(得分:10)

您没有看到任何单元格,因为您已生成了一个UICollectionViewController子类,它在viewDidLoad:中添加了以下行。根据我的理解,这会产生空白单元格,当使用自动布局时,它会被压缩到0,0。

删除此行以查看您在故事板中定义的单元格而不是隐藏的UICollectionViewCells

 // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

答案 1 :(得分:1)

就像周杰伦所说的那样,将这一行放在你的@interface下面它应该可行。您发布的代码没有错。

<UICollectionViewDelegate, UICollectionViewDataSource>

答案 2 :(得分:0)

您是否实现了UICollectionViewDelegate和UICollectionViewDataSource协议?

是否会调用collectionView:cellForItemAtIndexPath?

答案 3 :(得分:0)

如果您正在使用自定义collectionViewItems并且您想要将数据从nib文件寄存器第一个nib加载到collectionView:

let nib = UINib(nibName: reuseIdentifier, bundle: Bundle.main);
self.collectionView?.register(nib, forCellWithReuseIdentifier: reuseIdentifier);

并在集合视图委托中调用:

collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)