2014-03-25 30 views
3

我創建了一個UICollectionViewCell看起來像這樣UICollectionViewCell無法識別的選擇發送到實例

#import "PhotoCell.h" 


@implementation PhotoCell 

@synthesize imageView; 

- (instancetype)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 
     self.backgroundColor = [UIColor clearColor]; 

     imageView = [[UIImageView alloc] initWithFrame:(CGRect){.origin = CGPointMake(4.0f, 4.0f), .size=CGRectInset(frame, 4.0f, 4.0f).size}]; 
     imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
     [self.contentView addSubview:imageView]; 
    } 
    return self; 
} 

@end 

我從我的視圖控制器,我正在嘗試建立一個UICollectionView ..之一調用這個這是我我叫這個類

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    PhotoCell *cell = (PhotoCell *)[self.photoCollectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; 

    NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row]; 
    UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:@"DImage"]]; 

    // but I have no idea where to pass the imag.... 

    cell.imageView.image = imageForCollection; 

    return cell; 
} 

問題是,我得到這個錯誤,然後我的應用程序崩潰在堆中。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell imageView]: unrecognized selector sent to instance 0x16dc1a90' 

任何幫助解決這個問題將不勝感激!

UPDATE:

我創建了一個筆尖文件調用aPhotoCell和我從它刪除的UIView並添加一個UIImageView ..然後我已經改變了對象自定義類以光電,現在,我可以看到從PhotoCell的UIImageView IBOutlet我已經迷上了......但我仍然在接受相同的錯誤......我希望這是有道理的......從下面的評論和答案這是我認爲我必須做的修復..任何方式我會繼續觀看小費和答案。

+1

似乎是委託方法 - (UICollectionViewCell *)的CollectionView:(UICollectionView *)的CollectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath沒有返回光電對象 – bhawesh

回答

4

夫婦..

在你PhotoCell類 - 聲明的公共屬性:

@property (weak, nonatomic) IBOutlet UIImageView *imageView; 

然後在你的PhotoCell xib(我想這是你的出發創造它?)它連接到您創建的IBOutLet

我認爲你正在做的事情錯在你的cellForItemAtIndexPath - 因爲你的錯誤顯示UICollectionViewCell實例並不cell.imageView.image = imageForCollection;

應對確保您的小區標識也可作爲你把它放在你的故事板相同/ XIB文件 重寫你的cellForItemAtIndexPath方法是這樣的:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;{ 
PhotoCell *cell = [ self.photoCollectionView dequeueReusableCellWithReuseIdentifier:@"CORRECT CELL IDENTIFIER HERE" forIndexPath:indexPath]; 

NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row]; 
UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:@"DImage"]]; 


cell.imageView.image = imageForCollection; 

return cell; 
} 

編輯

看起來你創建了一個UICollectionViewCell的子類,並且從來沒有爲它創建一個xib文件。既然你不使用故事板這樣做:

在Xcode中:

創建一個新的文件 - 從下'iOS'左側選擇「User Interface」,並從它表明你的選擇選擇"empty"

創建您的xib文件後 - 轉到該文件並刪除UIView。然後從你右邊的對象庫,尋找UICollectionView Cell並將其拖動到視圖,然後拖動UIImageView並將其放置在你的UICollectionView cell.

現在選擇UICollectionViewCell剛纔創建和它的類設置爲PhotoCell類。像上面一樣連接IBOutlets。 請務必設置'Reusable Cell'屬性。這裏很重要。

然後在加載viewControllerUICollectionView - 它添加到viewDidLoad

 [photoCollectionView registerNib:[UINib nibWithNibName:@"THE XIB FILE NAME YOU JUST CREATED eg: PhotoCell" bundle:nil] forCellWithReuseIdentifier:@"REUSE IDENTIFIER HERE"]; 

這會幫助你。

+0

我沒有這個課的故事板或筆尖..當我創建它時,我沒有給它一個選項。我不知道如何繼續。 – HurkNburkS

+0

我會更新我的答案.. – Tander

+0

謝謝..事情是我已經嘗試創建一個xib然後連接它使用自定義類和連接IBOutlet,但我仍然recive相同的錯誤。 – HurkNburkS

0
  1. UICollectionView Class ReferenceCollectionViewCell只是沒有imageView財產。您應該先將其添加到Storyboard中的單元格contentView中。

  2. 此外,您正在使用自己的PhotoCell類。檢查您的單元格在Storyboard中的類名是否正確。

+0

我沒有故事板.... 我該怎麼辦? – HurkNburkS

0

我假設由「單元」標識的單元是在Xib文件中定義的。如果是這樣,我懷疑這個錯誤是因爲Xib文件中的主視圖是用UICollectionViewCell類定義的,而不是你定製的PhotoCell類。

編輯:
得知你不使用廈門國際銀行或故事板之後,什麼情況是,dequeueReusableCellWithReuseIdentifier沒有找到與becouse你還沒有在任何地方定義它給定的標識符「細胞」的細胞。從文檔:

必須調用此方法 之前註冊使用 registerNib:forCellReuseIdentifier:registerClass:forCellReuseIdentifier:方法的類或筆尖文件。

在你的情況下,你應該使用registerClass:...註冊你的類與「單元格」或任何其他標識符。事情

+0

我沒有這個類的xib文件,因爲UICOllectionViewCell在創建它們時沒有xibs ..它們允許你添加它們,所以我不知道該怎麼辦? – HurkNburkS

+0

查看編輯答案! – Merlevede

0

光電廈門國際銀行文件需要設置「單元格」,在標識 你的代碼應該是:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    PhotoCell *cell = (PhotoCell *)[self.photoCollectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; 
    if (cell == nil) { 
     // Load the top-level objects from the custom cell XIB. 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"cell" owner:self options:nil]; 
    // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
    cell = [topLevelObjects objectAtIndex:0]; 
    } 
    NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row]; 
    UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:@"DImage"]]; 

    // but I have no idea where to pass the imag.... 

    cell.imageView.image = imageForCollection; 

    return cell; 

}

+0

我現在創建了一個nib文件,將其自定義類更改爲PhotoCell,並連接了我製作的UIImageView IBOutlet。我仍然收到同樣的錯誤...我現在正在尋找更多的想法。 – HurkNburkS

+0

我已經更新了我的答案。 – HoanNguyen

4

我也有這個問題,我的問題是,我在我的代碼中的錯誤另一個地方。

我用這段代碼在viewDidLoad方法中註冊了UICollectionViewCell。

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuseLabel"]; 

但是,當我嘗試更新單元格與我的自定義UICollectionViewCell時,我沒有更改此行來工作我的自定義單元格。所以我改變了代碼如下:

[self.collectionView registerClass:[MyCustomCell class] forCellWithReuseIdentifier:@"reuseLabel"]; 
相關問題