调整未选定单元格 UI 的大小



我有一个包含 9 个可见项目的 UICollectionView。

当我选择一个单元格时,它的宽度和高度会发生变化,但不会更改所有其他未选择的单元格。

当我选择一个单元格时,我需要所有其他未选中的单元格都需要消失或将其帧归零。

同样在我的单元格中,我有一个按钮,可以将所选单元格返回到起始值,在这种情况下,所有其他单元格必须返回到原始大小。

谁能帮我理解这一点?

这是我用来展开所选单元格的代码

 @property (nonatomic, strong) NSIndexPath *selectedIndex;
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        if  (self.selectedIndex== indexPath) {
            // CELL SELECTED CHANGE SIZE
            return CGSizeMake(collectionView.frame.size.width, collectionView.frame.size.height);
        } 
        // ORIGINAL SIZE 
        return CGSizeMake(collectionView.frame.size.width/3, collectionView.frame.size.height/3);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedIndex = indexPath;
    [collectionView performBatchUpdates:nil completion:nil];
}
这个

怎么样

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    for (UICollectionViewCell* cell in collectionView.visibleCells) {
        if (cell.selected) {//leave only selected cell visible
            cell.hidden=NO;
        }
        else{
            cell.hidden=YES;
        }
    }
}

恢复取消选择时的可见性

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    for (UICollectionViewCell* cell in collectionView.visibleCells) {
        cell.hidden=NO;
    }
}

非常简单的方法,只是为了给你一个想法。

即,如果您的集合视图正在滚动,您可能会处理选定/未选定单元格的可见性

-(void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(nonnull UICollectionViewCell *)cell forItemAtIndexPath:(nonnull NSIndexPath *)indexPath

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(nonnull UICollectionViewCell *)cell forItemAtIndexPath:(nonnull NSIndexPath *)indexPath

最新更新