简体   繁体   English

UICollectionView:单元格选择上的minimumInteritemSpacing更改

[英]UICollectionView: minimumInteritemSpacing change on cell selection

In my collection view I want to scale the collection view cell that is selected. 在我的收藏夹视图中,我要缩放所选的收藏夹视图单元格。 Along with that I also want to maintain same interItemSpacing between cells before the selection and after the selection, including enlarged cell. 除此之外,我还想在选择之前和选择之后的单元格之间保持相同的interItemSpacing,包括扩大的单元格。 But if I just scale the cell by setting cell.transform property scaled cell overlaps on the neighbor cell.Other cells should adjust themselves to maintain the space. 但是如果我只是通过设置cell.transform属性来缩放单元格,则缩放后的单元格会在相邻单元格上重叠。其他单元格应该自行调整以保持空间。 How do I solve this ? 我该如何解决?

ie,

  cell -50pix- cell -50pix- cell

  cell -50pix- scaled cell -50pix- cell

By applying a transform, you are just scaling the view up in size. 通过应用变换,您只是在放大视图的大小。 To adjust the size and allow the rest of the UICollectionView to adjust you will need to return a new size in sizeForItemAtIndexPath: 要调整大小并允许其余的UICollectionView进行调整,您将需要在sizeForItemAtIndexPath中返回新的大小:

When the item is selected, you should reload that item at that indexPath using: 选择项目后,应使用以下命令在该indexPath上重新加载该项目:

 [collectionView reloadItemAtIndexPath:selectedIndexPath]

Replace selectedIndexPath with name of your variable storing the selected indexPath. 用存储所选indexPath的变量名称替换selectedIndexPath。

You don't want to invalidateLayout because you're just changing the appearance of one item. 您不希望使invalidateLayout无效,因为您只是在更改一项的外观。

I don't know whether this is the best solution, 我不知道这是否是最好的解决方案,

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        CollectionViewCell *cell = (CollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor redColor];
        return cell;
    }

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

        if ([indexPath isEqual:selectedIndexPath]) {
            return CGSizeMake(175, 175);  // return enlarged size if selected cell
        }
        return CGSizeMake(150, 150);
    }
    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
        selectedIndexPath = indexPath;   // set selected indexPath
        [collectionView performBatchUpdates:^{
                [collectionView.collectionViewLayout invalidateLayout];
            } completion:^(BOOL finished) {
                }];
    }

    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
        return UIEdgeInsetsMake(0, 0,0, 0);
    }

    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
        return 20;
    }
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
        return 20;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM