UICollectionView randomally不显示单元格

时间:2013-12-17 09:21:14

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

我有一个垂直滚动的UICollectionView。它的第一个单元格拥有另一个水平滚动的UICollectionView。

在随机情况下,水平UICollectionView会加载一个单元格,但不会显示它。 所以,我能看到的是几个单元格,空白区域和更多单元格。 我很难找到原因。

守则如下:

- (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.
}

第二部分是:

 // 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);
                                   }];
}

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

这听起来像是竞争条件,当数据源仍然没有可用数据时,UICollectionView正在尝试填充单元格。稍后当您向外滚动并返回时,将使用现在存在的数据填充单元格。