在collectionview上滚动后dequeueReusableCellWithReuseIdentifier出错了

时间:2014-05-07 14:55:58

标签: ios uicollectionview

enter image description here

我在UICollectionViewCell中遇到了UICollectionView的问题: 最后一个UICollectionViewCell没有填充13 - 18,而是第一个被复制。 我知道它与“dequeueReusableCellWithReuseIdentifier”有关,但如果我不对下面的标签进行检查,我的整个集合就会搞砸了。

我一直在努力解决这个问题并且谷歌搜索了大约一天,但我找不到如何解决这个问题。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID=@"pageCell";
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    int no = indexPath.item;
    if(collectionView == _pageCollectionView)
    {
        //Collection pages
        cell.tag=no+1;
        UICollectionViewFlowLayout *layoutMods=[[UICollectionViewFlowLayout alloc] init];
        [layoutMods setSectionInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        [layoutMods setMinimumInteritemSpacing:0];
        [layoutMods setMinimumLineSpacing:0];
        [layoutMods setItemSize:CGSizeMake(125, 125)];
        _modsCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0,0, _pageCollectionView.frame.size.width,  _pageCollectionView.frame.size.height) collectionViewLayout:layoutMods];
        [_modsCollectionView setDataSource:self];
        [_modsCollectionView setDelegate:self];
        _modsCollectionView.pagingEnabled = YES;
        [_modsCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"pageCell"];

        NSMutableArray *randColor = [[NSMutableArray alloc] init];
        for(int i=0;i<3;i++)
        {
            int lowerBound = 0;
            int upperBound = 255;
            NSNumber *rndValue = [NSNumber numberWithFloat:(lowerBound + arc4random() % (upperBound - lowerBound) )/255.0];
            [randColor insertObject:rndValue atIndex:i];
        }

        float r = [[randColor objectAtIndex:0] floatValue];
        float g = [[randColor objectAtIndex:1] floatValue];
        float b = [[randColor objectAtIndex:2] floatValue];
        _modsCollectionView.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1];
        [cell addSubview:_modsCollectionView];
    }
    else
    {
        if(cell.tag == 0)
        {
            cell.tag = 2;

            NSMutableDictionary *mod=[modules objectAtIndex:modulePointer];

            cell.backgroundColor=[mod objectForKey:@"color"];

            NSString *slug = [mod objectForKey:@"slug"];

            NSString *imgName=[NSString stringWithFormat:slug,@".png"];
            UIImageView *customBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]];
            customBackground.frame = CGRectMake(0,0, 125, 125);
            [cell addSubview:customBackground];
            customBackground.contentMode = UIViewContentModeScaleAspectFit;
            customBackground.clipsToBounds = YES;
            modulePointer++;
        }
    }
    return cell;
}

我做错了什么?

2 个答案:

答案 0 :(得分:1)

知道了!我必须使用- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath内的视图标记来完成一些魔术。

if(collectionView == _pageCollectionView)我把:

for(id subview in [cell subviews])
{
      [subview removeFromSuperview];
}
cell.tag=itemsPerScreen*no;

然后在以下else中:

int thisIndex=[collectionView superview].tag+no;
NSMutableDictionary *mod=[modules objectAtIndex:thisIndex];

答案 1 :(得分:0)

您需要创建自己的自定义UICollectionViewCell以防止这种情况发生。