UICollectionView not showing pictures [duplicate]

2019-03-06 21:32发布

This question is an exact duplicate of:

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;
    }

Feel free to as for more code.

1条回答
甜甜的少女心
2楼-- · 2019-03-06 22:01

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

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]; 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?

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.

查看更多
登录 后发表回答