簡體   English   中英

如何在iOS中使用Storyboard創建自定義UICollectionViewCell?

[英]How to create custom UICollectionViewCell using Storyboard in iOS?

我有一個UIViewController中,我有一個UICollectionView中的.The細胞UICollectionView必須是圖像,所以我明白,我需要繼承UICollectionViewCell類。 因此,基本上,我需要一個UIViewController,該UIViewController的UICollectionView帶有一個自定義UICollectionViewCell。 我正在使用情節提要,並且在需要創建自定義UITableVieCell ,請執行以下操作:

  • 創建UITableViewCell的子類
  • 將此子類設置為情節UITableViewCell中的UITableViewCell的類
  • 設置重用標識符。
  • 將該類導入我的視圖控制器實現中,聲明此子類的單元格並繼續。

但是當我對UICollectionViewCell遵循相同的UICollectionViewCell例如。 我創建一個名為CustomCollectionViewCell的子類,在cellForItemAtIndexPath聲明它

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

NSLog(@"%@",cell);

這會記錄<UICollectionViewCell: 0x137529010; frame = (10 10; 300 164); layer = <CALayer: 0x170220880>> <UICollectionViewCell: 0x137529010; frame = (10 10; 300 164); layer = <CALayer: 0x170220880>> <UICollectionViewCell: 0x137529010; frame = (10 10; 300 164); layer = <CALayer: 0x170220880>>不管我做什么。 我無法初始化我的自定義UICollectionViewCell類,因此無法添加圖像。

我查看了其他StackOverflow答案,並閱讀了答案,說不這樣做:

[self.collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];

如果我使用情節提要,那么我將其注釋掉。

所有重用標識符,出口等均已設置。 我似乎找不到錯誤所在。

任何幫助表示贊賞。

嘗試這個,

Identity Inspector設置您的自定義collectionview單元格類,例如

在此處輸入圖片說明

並在cellForItemAtIndexPath

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

    CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSLog(@"%@", cell);
    return cell;
}

並獲得日志為

<CollectionViewCell: 0x7af70000; baseClass = UICollectionViewCell; frame = (0 0; 70 69); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7af70210>>

我想到了。

這是一個愚蠢的錯誤。 我正在使用的重用標識符已被其他地方的其他單元使用。 對其進行了更改,現在可以完美運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM