2014-10-10 52 views
0

我試圖從UICollectionView文件目錄顯示圖像的任何圖像,一切工作正常,但似乎沒有圖像UICollectionView不顯示

- (void)viewDidLoad 

{ 

    [super viewDidLoad]; 

    // Initialize recipe image array 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 



    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil]; 


    NSMutableArray *image = [NSMutableArray array]; 


    for (NSString *tString in dirContents) { 

     if ([tString hasSuffix:@".png"]) { 

      [image addObject:tString]; 


      listeImages = [NSArray arrayWithArray:image]; 


     } 

    } 


} 


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

    return listeImages.count; 



} 


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

    static NSString *identifier = @"Cell"; 

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 



    UIImageView *listeImageView = (UIImageView *)[cell viewWithTag:100]; 

    UILabel *LabelView = (UILabel *)[cell viewWithTag:110]; 

    LabelView.text = [listeImages objectAtIndex:indexPath.row]; 

    listeImageView.image = [UIImage imageNamed:[listeImages objectAtIndex:indexPath.row]]; 

    NSLog(@" List UIImageView listeImageView.image %@",listeImageView.image); 

cell.backgroundColor =的UIColor redColor]

cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]]; 



    NSLog(@" List NSArray *listeImages %@",listeImages); 

    cell.tag = indexPath.row; 

    return cell; 

} 

與此一段代碼,顯示正確的UICollectionView的UILabel良好,但不顯示圖像。

我使用這行代碼將我的圖像存儲在listeImageView.image中。

UIImageView *listeImageView = (UIImageView *)[cell viewWithTag:100]; 
    listeImageView.image = [UIImage imageNamed:[listeImages objectAtIndex:indexPath.row]]; 

這裏是我的NSLog的結果:

2014-10-11 09:46:31.117 imgapp[2803:60b] List NSArray *listeImages; (
    "image10102014233607.png", 
    "image10102014233616.png", 
    "image10102014233627.png" 
) 
2014-10-11 09:46:31.158 imgapp[2803:60b] List UIImageView listeImageView.image (null) 
2014-10-11 09:46:31.171 imgapp[2803:60b] List UIImageView listeImageView.image (null) 
2014-10-11 09:46:31.178 imgapp[2803:60b] List UIImageView listeImageView.image (null) 

任何幫助將不勝感激。

感謝

+0

你的NSLog語句在numberOfItemsInSection永遠不會被調用。返回語句後不應該有代碼。交換這兩條線。 – TMob 2014-10-10 07:28:38

回答

0

你不加入你的listeImageView任何東西。 與

[cell addSubview:listeImageView]; 

嘗試它,或者如果你真的想成爲backgroundView

cell.backgroundView = listeImageView; 

但它會是乾淨多了,如果你創建一個自定義UICollectionViewCell級,你可以定義哪些內容該細胞有。

+0

謝謝你的回答,但我用這行代碼'code'UIImageView * listeImageView =(UIImageView *)[cell viewWithTag:100]添加我的listeImageView單元格;'代碼' 我嘗試你的代碼行,但是是相同的結果。 – IndiaNasirJones 2014-10-11 07:36:59

1

您擁有以下,

listeImageView.image = [UIImage imageNamed:[listeImages objectAtIndex:indexPath.row]]; 

但你這樣做,

cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]]; 

爲什麼你不使用如下的listeImageView: -

cell.backgroundView = listeImageView; 

這應該讓你的形象。

更新: -

for (NSString *tString in dirContents) { 

    if ([tString hasSuffix:@".png"]) { 

       **[image addObject:[UIImage imageWithContentsOfFile:tString] ]; //Do this as in below line you were not create a image object which you are trying to get.** 

     //Also check your image array for object, that is it getting stored or not. 
     //  [image addObject:tString]; 

    } 

} 

listeImages = [NSArray arrayWithArray:image]; //Do this after you have added all objects in your image mutable array and not in for loop as this will initialise it many tiems depending on your loop run count. 
+0

謝謝你的回答,但行代碼'code'cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@「photo-frame.png」]];'代碼'僅用於顯示任意圖像的相框它正在工作。 – IndiaNasirJones 2014-10-11 07:38:22

+0

@IndiaNasirJones所以如果你的查詢已解決,那麼你可以關閉你的查詢。 – nikhil84 2014-10-11 09:11:01

+0

我只是用新元素編輯代碼,問題似乎是我的listeImageView不會儲存我的圖像,但我不明白爲什麼? – IndiaNasirJones 2014-10-11 09:28:16

0

具有u想給背景色而不是細胞backgroundview.do的,一旦如果有可能與你的相同的代碼!你可以嘗試像這樣:-UIImageView * listeImageView =(UIImageView *)[cell viewWithTag:100]; [cell bringviewtoFront:listeImageView];

0

你可以在子類化UICollectionViewCell,並在故事板中設置子視圖好多了。您的代碼可能會更清潔這種方式:

static NSString *kCustomCellIdentifier = @"customcell"; 

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

    // Load custom image 
    NSString *imageName = [listeImages objectAtIndex:indexPath.row]; 
    if (imageName) 
    { 
     UIImage *image = [UIImage imageNamed:imageName]; 
     if (image) 
     { 
      cell.customImageView.image = image; 
     } 
     else 
     { 
      // Set an empty state here 
      cell.customImageView.image = [UIImage imageNamed:@"errorImage.png"]; 
     } 
    } 

    return cell; 
}