【问题标题】:UICollectionView is not loadingUICollectionView 未加载
【发布时间】:2018-01-24 08:37:33
【问题描述】:

这是我在 cellForItemAtIndexPath 中的代码,我想要两个 collectionView 的负载单元。这里第一个加载完美,但第二个加载正确。

} else if (collectionView == _collBanner) {

    static NSString *cellIdentifier = @"bannerCell";

    NSDictionary *dictBanner = [arrImages objectAtIndex:indexPath.row];

    BannerCollectionViewCell *cell = (BannerCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    [collectionView registerNib:[UINib nibWithNibName:@"BannerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellIdentifier];

    [cell setupBannerCell:dictBanner];

    return cell;
}
return nil;
}

而我的 sizeForItemAtIndexPath 方法是……

} else if (collectionView == _collBanner) {

    return CGSizeMake(_collBanner.frame.size.width -5, _collBanner.frame.size.height -2);
} else
    return CGSizeMake(0, 0);
}

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell


    【解决方案1】:

    此行必须在 viewDidLoad 中

        [_collBanner registerNib:[UINib nibWithNibName:@"BannerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellIdentifier];
    

    【讨论】:

    • 怎么做,因为如果我粘贴,这里会显示一些错误消息。
    • 声明这一行静态 NSString *cellIdentifier = @"bannerCell";在导入下方。
    【解决方案2】:

    问题的顺序如下两行:

    BannerCollectionViewCell *cell = (BannerCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
    [collectionView registerNib:[UINib nibWithNibName:@"BannerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellIdentifier];
    

    第一个请求BannerCollectionViewCell 类型的单元格,但要使collectionView 能够将其出列,BannerCollectionViewCell 必须在collectionView 中注册。只有第二行将单元格类型注册到collectionView

    首先你必须将BannerCollectionViewCell类型注册到collectionView:

    [collectionView registerNib:[UINib nibWithNibName:@"BannerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellIdentifier];
    

    只有这样你才能使用以下方法使单元格出队:

    BannerCollectionViewCell *cell = (BannerCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    

    但是,只注册一次单元格就足够了,因此最好的方法是在viewDidLoad中注册单元格(将以下行从cellForItemAt移动到viewDidLoad):

    [collectionView registerNib:[UINib nibWithNibName:@"BannerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellIdentifier];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-02
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      相关资源
      最近更新 更多