UICollectionView不显示图片 [英] UICollectionView not showing pictures

查看:166
本文介绍了UICollectionView不显示图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在所有的目录中显示图片,但它不显示图片。我把NSLog放在代码中,以便我可以找出哪个代码正在工作,而且我在日志中只得到j。我没有在日志中看到a。你觉得是错的?

I am showing the Pictures in all of the Directories however It does not display the pictures. I am putting NSLog in the code so that I can find out which code is working and I only get "j" in the log. I do not see the "a" in the log. What do you think is wrong?

 - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        allImagesArray = [[NSMutableArray alloc] init];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSArray *locations = [[NSArray alloc]initWithObjects:@"Bottoms", @"Dress", @"Coats", @"Others", @"hats", @"Tops",nil ];
        NSString *fPath = documentsDirectory;
        for(NSString *component in locations)
        {
            fPath = [fPath stringByAppendingPathComponent:component];
        }
        NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
        collectionTrash.delegate =self;
        collectionTrash.dataSource=self;
        for(NSString *str in directoryContent){
            NSLog(@"i");
            NSString *finalFilePath = [fPath stringByAppendingPathComponent:str];
            NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
            if(data)
            {
                UIImage *image = [UIImage imageWithData:data];
                [allImagesArray addObject:image];
                NSLog(@"array:%@",[allImagesArray description]);
            }}
        for(NSString *folder in locations) {

            // get the folder contents

            for(NSString *file in directoryContent) {

                // load the image

            }
        }}

    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
    {
        NSLog(@"j");
        return 1;
    }

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return [allImagesArray count];


    }


    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *reuseID = @"ReuseID";
        TrashCell *mycell = (TrashCell *) [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
        UIImageView *imageInCell = (UIImageView*)[mycell viewWithTag:1];
        imageInCell.image = [allImagesArray objectAtIndex:indexPath.row];
        NSLog(@"a");
        return mycell;
    }

随意更多代码。

推荐答案

如果你看异常,它会非常准确地告诉你什么是错误的:

If you look at the exception, it tells you very precisely what's wrong:

你正在尝试在数组上调用一个length方法,这个方法根本不存在。您想要使用计数。它不在你发布的代码中 - 所以只需搜索 length ,如果项目不是很大,你可能会很容易找到它。

You are trying to calling a 'length' method on an array, which simply does not exist. You want to use count here instead. It's not in the code you posted, though - so just do a search for length and you'll probably find it rather easily if the project isn't huge.

NSString * fPath = [documentsDirectory stringByAppendingPathComponent:locations]; 给你一个警告,因为 / code>是一个数组。想想一下 - 只是没有意义:你要添加哪些元素到路径?

NSString *fPath = [documentsDirectory stringByAppendingPathComponent:locations]; gives you a warning, because locations is an array. Think about it - just doesn't make sense: which of its elements do you want to add to the path?

正如我所想,这两个错误都可能与彼此之间:您简单地忽略了fPath的编译时错误或警告,而现在的 stringByAppendingPathComponent:方法调用其参数的长度,这是一个预期的方法 NSString

As I think about it, both errors probably relate to each other: You simply ignored the compile time error - or warning - for the fPath, and now the stringByAppendingPathComponent: method calls length on its parameter, which is a method of the expected NSString.

底线:不要忽略编译器警告!如果您修复这些问题,您可能也会减少崩溃。

Bottom line: Do not ignore compiler warnings! If you fix those, you probably reduce crashes, too.

这篇关于UICollectionView不显示图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆