滑动 CollectionView 图片混乱问题

来源:互联网 发布:eclipse端口号 编辑:程序博客网 时间:2024/05/21 09:05

在开发中我遇到一个问题就是滑动 CollectionView 图片混乱问题

项目首页我用一个 Collectionview 实现三个分组,并且实现 cell 大小不一致的分配,但是在我滑动 Collectionview 的时候,第一组左边那两张大图片大小一直在变,而且其他图片也发生了的错乱,如图




用尽各种方法终于找到了解决办法,来给大家分享一下,希望对小伙伴以后的开发有帮助

1、第一组的 cell 我注册了两次,但是注意:cellId 是不一样的

//第一组左边两个图片 [_collectionView registerClass:[XLNewProductCollectionViewCell class] forCellWithReuseIdentifier:@"myCell1-1"]; //第一组其他图片 [_collectionView registerClass:[XLNewProductCollectionViewCell class]            forCellWithReuseIdentifier:@"myCell1-2"];


2、然后,在代理复用方法中实现

    static NSString *cellId11 = @"myCell1-1";    static NSString *cellId12 = @"myCell1-2";    static NSString *cellId2 = @"myCell2";    static NSString *cellId3 = @"myCell3";        if (indexPath.section == 0) {        if (indexPath.row % 4 == 0) { //第一组左边两个图片            XLNewProductCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId11 forIndexPath:indexPath];                        return cell;        }else{ //第一组其他图片            XLNewProductCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId12 forIndexPath:indexPath];                        return cell;        }    }else if (indexPath.section == 1){        XLHotProductCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId2 forIndexPath:indexPath];                return cell;    }else{        XLRecommedCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId3 forIndexPath:indexPath];                return cell;    }



3、最后在代理方法中实现 Item 大小的设置

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.section == 0) {        if (indexPath.row % 4 == 0) { //第一组左边两张图片 Item 的大小            return CGSizeMake((SCREEN_W-3)/5*2, 90);        }else{ //第一组其他图片 Item 大小            return CGSizeMake((SCREEN_W-4.5)/5, 90);        }    }else if (indexPath.section == 1){        return CGSizeMake((SCREEN_W-60)/3, (SCREEN_W-60)/3+30);    }else{        return CGSizeMake((SCREEN_W-55)/2, (SCREEN_W-55)/2+5);    }}

修改之后的效果图如下


如果觉得有用,请不要吝啬自己的手,帮忙顶一个,嘿嘿。

如果有更好的办法,欢迎大家来分享

1 0
原创粉丝点击