2013-03-11 63 views
0

我正在嘗試使用UICollectionViewUICollectionViewCell合作顯示圖像的縮略圖。在我的應用程序中使用的UICollectionViewCell的是自定義的(簡單)的子類:UICollectionView崩潰?

#import "MemeCell.h" 

@implementation MemeCell 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 

} 
return self; 
} 

-(void)setThumb:(UIImage *)image { 
    if (_thumb != image) { 
     _thumb = image; 
    } 

    _imageThumb.image = _thumb; 
} 

@end 

在我UICollectionViewFile's Owner,我使用:

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

    static BOOL nibLoaded = NO; 

if (!nibLoaded) { 
    UINib *cellNib = [UINib nibWithNibName:@"MemeCell" bundle:nil]; 
    [_collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MemeCell"]; 
    nibLoaded = YES; 
} 

MemeCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MemeCell" forIndexPath:indexPath]; 

NSString *path = [_imageThumbs objectAtIndex:indexPath.section]; 
UIImage *thumb = [UIImage imageNamed:path]; 
[cell setThumb:thumb]; 

return cell; 
} 

返回一個細胞。該視圖正常工作時,第一次提出了,但是從中的代表呼籲[self dismissViewControllerAnimated:YES completion:nil]解僱本身後,不能再沒有與

2013-03-10 22:11:35.448 CapifyPro[21115:907] -[UICollectionViewCell setThumb:]: unrecognized selector sent to instance 0x1e593320 
2013-03-10 22:11:35.450 CapifyPro[21115:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell setThumb:]: unrecognized selector sent to instance 0x1e593320' 

崩潰誰能洞察呈現的?

+0

錯誤消息是說你正在嘗試將setThumb發送到UICollectionViewCell,而不是你的自定義單元格。你是否正在用這個單元在IB中做任何事情,或者你是否爲單元註冊了一個班級或筆尖? – rdelmar 2013-03-11 02:35:04

+0

我在'viewDidLoad'中註冊了一個類,並且將'collectionView:cellForItemAtIndexPath:'改爲了我編輯的文章 – HighFlyingFantasy 2013-03-11 02:37:24

+0

嘗試取出if(!nibLoaded)子句,並註冊nib而不是類。然後,如果沒有單元出隊,系統會自動從nib文件中獲取一個單元。 – rdelmar 2013-03-11 02:40:58

回答

0

如果使用XIB或storyBoardCell使用UICollectionViewCell。簡單的拖放你的imageThumb imageView到MemeCell.h。在MemeCell.m中刪除你的setter。然後設置順序:

MemeCell *cell = (MemeCell*) [cv dequeueReusableCellWithReuseIdentifier:@"MemeCell" forIndexPath:indexPath]; 
cell.imageThumb.image = [UIImage imageNamed:path] 

在MemeCell no Xib的情況下。請初始化並添加imageThumb作爲memeCell.contentView的子視圖。

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
    self.imageThumb = [[UIImageView alloc] initWithFrame:self.frame]; 
    [self.contentView addSubviews:self.imageThumb]; 

} 
return self; 
} 

*編輯:如果你只是存儲你的thumbImage,不要顯示,只需在Cell.h中聲明,不需要重寫setter。

property(strong,nonomatic) UIImage *thumbImage;