0

我正在我的应用程序中创建一个页面,其中分段控件显示两个不同的集合视图。CV 的数据来自包含两个不同长度数组的 API。

我的问题是第一个集合视图在数组中的项目比第二个多,第二个集合视图在 cellAtIndexPath 中的迭代次数超过了所需的次数,因此返回错误*由于未捕获的异常“NSRangeException”而终止应用程序,原因:'- [__NSCFArray objectAtIndex:]: 索引 (1) 越界 (1)'

任何人都可以帮助我吗?

谢谢

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

DataStore *ds = [DataStore sharedDataStore];


if (self.cvActive) {
    return ds.arLikesActive.count;

}
else {
    return ds.arLikesExpired.count;
}

}

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

DataStore *ds = [DataStore sharedDataStore];

if (collectionView == self.cvActive) {

    CollectionViewCellLikedActive *cell = (CollectionViewCellLikedActive *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    self.dProductActive = ds.arLikesActive[indexPath.item];

    cell.ivActiveProduct.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.dProductActive[@"ImageURL"]]]];

    return cell;

}
else {

    CollectionViewCellLikedWhereAreTheyNow *cell = (CollectionViewCellLikedWhereAreTheyNow *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    self.dProductInactive = ds.arLikesExpired[indexPath.item];

    cell.ivWhereAreTheyNowProduct.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.dProductInactive[@"ImageURL"]]]];

    return cell;

}
4

1 回答 1

1

我认为问题在于您在 numberOfItemsInSection 方法中的 if 语句,

if (self.cvActive) {

这不是总是会评估为真吗?它应该看起来就像你在 cellForItemAtIndexPath 中的那个,

if (collectionView == self.cvActive) {
于 2014-06-14T15:57:58.913 回答