簡體   English   中英

UIImageView不會在UICollectionViewCell中加載圖像

[英]UIImageView won't load image in UICollectionViewCell

我是UICollectionViews的新手。 我已經設置了一個基本的UICollectionViewController,其中包含2個單元格的行。 在控制器中,我有一個單元格,並將一個UIImageView拖到其中。 我將其連接到自定義單元格類。 我確保在界面生成器中也將單元格的類更改為這一類。 在我的視圖控制器中,我有以下代碼:

@implementation StickersViewController

static NSString * const reuseIdentifier = @"stickerCell";

- (void)viewDidLoad {
    [super viewDidLoad];




    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];


    //Add the images
    self.images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"one.png"], [UIImage imageNamed:@"two.png"], [UIImage imageNamed:@"three.png"], nil];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark <UICollectionViewDataSource>

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

    return 1;
}


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

    return [self.images count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    StickerCell *cell = (StickerCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    cell.imageView.image=self.images[indexPath.row];

    return cell;
}

運行代碼時出現此錯誤: UICollectionViewCell imageView]:無法識別的選擇器發送到實例0x7fe8e0e7828 不知道為什么。 似乎我已正確連接東西。 有人可以給我一些指示我可能出問題的地方嗎?

問題在這里:

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

在其中注冊UICollectionViewCell而不是自定義子類的位置。 因此應該是:

[self.collectionView registerClass:[StickerCell class] forCellWithReuseIdentifier:reuseIdentifier];

另外,請記住,如果在情節提要中完成設置,則無需自己注冊該類,因為它會自動為您處理(前提是您已設置了自定義類和重用標識符)

我希望這是有道理的

請參閱本教程http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
您會得到正確的答案。 可能我認為您沒有設置集合可重用的視圖標識符
選擇集合視圖單元格轉到屬性檢查器,並設置集合可重用視圖標識符=單元格

暫無
暫無

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

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