UICollectionView does not randomly display a cell

I have a UICollectionView that scrolls vertically. The first cell contains another UICollectionView that scrolls horizontally.

In random cases, the horizontal UICollectionView loads the cell and does not display it. So, I can see a couple of cells, empty space and a few more cells. It’s hard for me to understand the reason for this.

The code is as follows:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView.tag == 1) {

    if (indexPath.section == 1) {

// do some stuff

} else if (collectionView.tag == 999) { // horizontal collection view

    HorizontalCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell999" forIndexPath:indexPath];

    [self addBorderToCellImageView:cell.imageView];

    [self updateHorizontalCell:cell forIndexPath:indexPath];

    return cell;
}

return [[UICollectionViewCell alloc] init]; // in any case, return a cell.
}

second part:

 // updating cells for collectionView.tag = 999;
- (void)updateHorizontalCell:(HorizontalCell *)cell forIndexPath:(NSIndexPath *)indexPath
{

id obj = self.horizontalProductDataSource[indexPath.row]; // use introspection, and act accordingly.
NSLog(@"section %ld, row %ld", (long)indexPath.section, (long)indexPath.row);

if ([obj isKindOfClass:[Product class]]) {

    Product *wishListProduct = (Product *)obj;

    cell.subtitle.hidden = YES;
    cell.title.text = wishListProduct.name;
    NSLog(@"from product: %@", wishListProduct.name);
    NSLog(@"from cell: %@", cell.title.text);
    cell.price.text = [NSString stringWithFormat:@"%.f %@", wishListProduct.totalPrice, NSLocalizedString(@"Shekel", nil)];

    __weak HorizontalCell *weakCell = cell;

    NSString *imageURL;

    Image *image = [wishListProduct.pictuers lastObject];
    imageURL = image.url;

    [cell.imageView setImageWithURLRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:imageURL]]
                          placeholderImage:[UIImage imageNamed:@"Icon.png"] 
                                   success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
                                       weakCell.imageView.image = image;
                                   }
                                   failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
                                       logIt(@"error while bringing photos into cells: %@", error);
                                   }];
}

any help would be awesome!

+4
source share
1 answer

, UICollectionView , . , , , .

0

All Articles