2014-10-10 55 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; 
}