2015-12-28 156 views
0

我有一个自定义的TagCollectionViewCell,它是一个自定义的UICollectionViewCell。在.xib的日子里,我曾经通过UINib实例化,但现在使用Storyboard我很无聊。CollectionViewCell问题。从.xib到自定义collectionviewcell的故事板转换

\t UINib *cellNib = [UINib nibWithNibName:@"TagCollectionViewCell" bundle:nil]; 
 
\t [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"TagCell"]; 
 
\t 
 
\t 
 
\t _sizingCell = [[cellNib instantiateWithOwner:nil options:nil] objectAtIndex:0];

在故事板贴我的笔尖在.storyboard,给它分配的自定义类。但我得到一个白色的屏幕。这是我的自定义收集单元,我希望它成为故事板。 .xib snapshot to be changed into the storyboard.

我应该在集合视图cell_for_item_atindexpath中做什么更改。 .xib的代码如下。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
 
{ 
 
\t TagCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TagCell" forIndexPath:indexPath]; 
 
\t 
 
\t [self _configureCell:cell forIndexPath:indexPath]; 
 
\t 
 
\t return cell; 
 
}

和配置布局是这样的。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
 
{ 
 
\t [self _configureCell:_sizingCell forIndexPath:indexPath]; 
 
\t 
 
\t return [_sizingCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 
 
}

+0

嗨。看到我的回答如下 – LearneriOS

回答

2

您还可以在故事板中使用自定义收藏视图单元格。

创建UICollectionViewCell的子类。

设置数据源和委托。

如图所示,在故事板中设置类和标识符。

enter image description here

enter image description here

+0

是的,我已经做到了。我在cellForItemAtIndexPath中缺少一些东西。使用故事板会有所不同吗?考虑**“dequeueReusableCellWithReuseIdentifier”**,我将提供故事板中使用的标识符,我做了那个,还有其他的东西我都没有。 –

+0

我明白了!我需要将它转换为(TagCollectionViewCell *) –

0

你有没有设置数据源,并为您的CollectionView委托。最常见的错误之一。告诉我是否需要更多帮助。

+0

是的,我怎么能忘记这一点。 –