【问题标题】:UICollectionView randomally not displaying a cellUICollectionView 随机不显示单元格
【发布时间】:2013-12-17 09:21:13
【问题描述】:

我有一个可以垂直滚动的 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);
                                   }];
}

任何帮助都会很棒!

【问题讨论】:

  • 你会尝试使用 tableview 和 collectionview 混合来进行水平和垂直滚动吗?
  • 什么意思? @DevangiDesai ?
  • raywenderlich.com/4680/… - 你可以试试这个例子
  • 我已经在这方面工作了。在我的应用程序中,它运行良好。我有一个 tableview + collectionview。它垂直滚动,每个单元格也水平滚动
  • 感谢@DevangiDesai,但这不是我的问题/问题。不是我不能显示任何东西,或者布局有问题。一切正常。问题是有时我“错过”了一个单元格......而且它是一个现有的项目,所以这些变化不能真正发生

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout


【解决方案1】:

这听起来像是一种竞争条件,当数据源仍然没有可用数据时,UICollectionView 试图填充单元格。稍后,当您向后滚动时,单元格会填充现在存在的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多