UICollectionView不显示任何图像

时间:2014-10-10 07:16:43

标签: ios objective-c uicollectionview uicollectionviewcell

我正在尝试在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)

任何帮助将不胜感激。

由于

4 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

您没有将 listeImageView 添加到任何内容中。 尝试使用

[cell addSubview:listeImageView];

或者如果你真的想要它是backgroundView

cell.backgroundView = listeImageView;

但如果你创建一个自定义的UICollectionViewCell类,你可以定义这个单元格的内容,它会更清晰。

答案 2 :(得分:0)

您是否尝试过将背景颜色添加到单元格而不是backgroundview.do,如果可能的话,请使用相同的代码!

或者 您可以尝试:-UIImageView * listeImageView =(UIImageView *)[cell viewWithTag:100]; [cell bringviewtoFront:listeImageView];

答案 3 :(得分: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;
}