2015-12-28 86 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

是的,我怎麼能忘記這一點。 –