2014-03-25 67 views
0

我創建了一個UICollectionView,我目前已經顯示出白色單元格。我一直在關注這個tutorial弄清楚如何使用委託方法等。如何添加照片到UICollectionViewCell

我做起來的地步創建創建自定義UICollectionViewCells然而,在這個例子中,教程,他會告訴你去開啓你的故事板等等。我沒有故事板...我試圖按照說明創建視圖,但是使用我自己的.xib文件,但我無法獲得任何工作。

我希望在這裏有人可以幫我看看我的委託方法在這裏。

// this is how I load the collection view 
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init]; 
    photoCollectionView =[[UICollectionView alloc] initWithFrame:CGRectMake(10.0, 40.0, 480.0, 713.0) collectionViewLayout:layout]; 

    photoCollectionView.dataSource = self; 
    photoCollectionView.delegate = self; 
    [self.view addSubview:photoCollectionView]; 

    [photoCollectionView.layer setBorderColor: [[UIColor lightGrayColor] CGColor]]; 
    [photoCollectionView.layer setBorderWidth: 1.0]; 
    [self.photoCollectionView reloadData]; 



// this is what my delegate methods look like 
#pragma mark - CollectionView Delegates 
#pragma mark -- UICollectionView Datasource 
// 1 
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { 
    return [imageArray count]; 
} 
// 2 
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView { 
    return 1; 
} 
// 3 
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath]; 

    // I think this is where I want to set my image for the cell 
    NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row]; 
    UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:@"DImage"]]; 

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

    cell.backgroundColor = [UIColor whiteColor]; 
    return cell; 
} 


#pragma mark -- UICollectionView Delegate 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // TODO: Select Item 
} 
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { 
    // TODO: Deselect item 
} 


#pragma mark –- UICollectionViewDelegate FlowLayout 

// 1 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 
    CGSize retval = CGSizeMake(210, 157+20); // add 20 for labels under neath.. might need to adjust this. 
    return retval; 
} 


- (UIEdgeInsets)collectionView: 
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { 
    return UIEdgeInsetsMake(50, 20, 50, 20); 


}Subview:photoCollectionView]; 

     [photoCollectionView.layer setBorderColor: [[UIColor lightGrayColor] CGColor]]; 
     [photoCollectionView.layer setBorderWidth: 1.0]; 
     [self.photoCollectionView reloadData]; 

,你可以在的CollectionView看到:cellForItemAtIndexPath我有我的形象準備

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

我只需要知道如何將它裝入collectionviewcell ...

任何幫助將不勝感激。

回答

0

請儘量使用像這樣的....

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

    // I think this is where I want to set my image for the cell 
    NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row]; 
    UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:@"DImage"]]; 
[cell setBackgroundColor:[UIColor colorWithPatternImage:imageForCollection];// Add your image here........ 

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

    cell.backgroundColor = [UIColor whiteColor]; 
    return cell; 
} 

我希望它會幫助你...

+0

代碼現在產生錯誤*****由於未捕獲的異常'NSInternalInconsistencyException'原因終止應用程序,原因:'無法使類型視圖脫離:UICollectionElementKindCell帶標識符PhotoCell - 必須爲標識符註冊一個筆尖或類別,或者在故事板中連接一個原型單元格** – HurkNburkS

+0

將內存分配給您的單元格的位置? –

0

我UICollectionViewCell與名稱光電。並將label和imageview作爲屬性。 這是你可以如何設置圖像單元格。可能這是你的要求與否。我不知道。試試這個

您需要在您的UICollectionViewCell作爲MY_CELL的廈門國際銀行或其他一些name.Then

.M

static NSString *[email protected]"MY_CELL"; 

設置標識符在viewDidLoad方法

UINib *nib; 
nib = [UINib nibWithNibName:@"PhotoCell" bundle:nil]; 
[self.collectionView registerNib:nib forCellWithReuseIdentifier:cellidentity]; 

我UICollectionViewCell。 h是

@interface PhotoCell : UICollectionViewCell 

@property (retain, nonatomic) IBOutlet UILabel* label; 

@property (retain, nonatomic) IBOutlet UILabel* timelineLabel; 

@property (retain, nonatomic) IBOutlet UIImageView* yourImageView; 

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    // PhotoCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"PhotoCell" //forIndexPath:indexPath]; 
PhotoCell *cell = [cv dequeueReusableCellWithReuseIdentifier:cellidentity 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.label.text = [NSString stringWithFormat:@"%d",indexPath.item]; 

    cell.yourImageView.image=imageForCollection; 

    return cell; 
} 

希望它可以幫助你...

+0

好吧我已經想通了如何做你已經問過我作爲一個錯誤雖然。 **因未捕獲異常'NSInternalInconsistencyException'而終止應用程序,原因:'無法取消類型視圖:具有標識符單元的UICollectionElementKindCell - 必須爲標識符註冊一個nib或類或連接故事板中的原型單元** ** – HurkNburkS

+0

@ HurkNburkS ..請參考我的更新答案解決方案。 – Vidhyanand