简体   繁体   中英

UICollectionView showing empty cells

I have a UICollectionView that displays an array of images. The images in the cell are being displayed as white boxes instead of the real image.

I copied and pasted this code from another view and it works great in the other view... but somehow in this other view its displaying all images like a white box.

The boxes shown are equal to the number of images that should be displayed. So the information is passed correctly.

The images are being displayed like litte white boxes but when I click on them they then display the selected image into another UIImage element that i placed somewhere else (see image1, the green square to the left of the clear button changes depending on which white cellIi touch).

Could anyone spot why is the UIImage in the Cell not displaying properly?

maybe is worth metioning that i also have a tableview in the same view

// in viewDidLoad method
[self.myCollection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cells"];

//
- (void)getAllIcons
{        
    NSArray* imgArrayRaw = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil];

    NSMutableArray* imgArray = [[NSMutableArray alloc] init];
    for (NSString* imgArrayProccesing in imgArrayRaw) {
        NSString* ho = [imgArrayProccesing lastPathComponent];
        if([ho rangeOfString:@"btn-"].location != NSNotFound){
            [imgArray addObject:ho];
        }
    }
    _imgFiles = [ NSMutableArray arrayWithObjects:imgArray,nil];
    _imgFiles = _imgFiles[0];


    _myCollection.hidden = NO;
    [_myCollection reloadData];
}

// Collection View
#pragma mark - UICollectionView Datasource

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

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{   
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cells" forIndexPath:indexPath];

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:101]; 
    imageView.image = [UIImage imageNamed:[_imgFiles objectAtIndex:indexPath.row]]; 
    cell.backgroundColor = [UIColor whiteColor];

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self changeIconImage:[_imgFiles objectAtIndex:indexPath.row]]; 
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{   
    return CGSizeMake(50, 50);  
}

- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{   
    return UIEdgeInsetsMake(50, 20, 50, 20);
}

Image1: 图片1在此输入图像描述在此输入图像描述

I'm sorry, you haven't provided enough information to completely diagnose the problem. I would suspect from your code one of the following possibilities;

a) The specified image has not been found in your project, and so has returned nil;

b) The imageView with tag 101 was not found and so returned nil;

c) The imageView was found, but was either not enabled or was hidden.

I would set a breakpoint at the "cell.backgroundColor" line and check each of these return values and check the status of the imageView to see that it is enabled and not hidden.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM