如何在选中时展开UICollectionView Cell而不会弄乱集合视图

时间:2017-06-01 01:26:51

标签: ios swift uicollectionview cell

目前我有一个我的选择来扩展我的每个单元格:

@objc(collectionView:didSelectItemAtIndexPath:) func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("selected")
    let cell = collectionView.cellForItem(at: indexPath)

    UIView.animate(withDuration: 0.5, animations: ({


        cell?.frame = CGRect(x: 0, y: 0, width: 331, height: 320)
       // cell?.superview?.bringSubview(toFront: cell!)

        }), completion: nil)

}

我遇到的问题是细胞开始重叠,而不是向下调整到大细胞大小。我希望能够点击任何单元格,它会扩展到更大的尺寸,而其他单元格向下移动以考虑此调整大小。单击第二个单元格将使第一个单元格变小。我该怎么做呢?这是一场斗争!

1 个答案:

答案 0 :(得分:0)

您应该在didSelected中调用reloadItems然后调整sizeForItemAtIndexPath中的项目:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"when SELECTED");
__weak MainCollectionViewCell *cell = (MainCollectionViewCell*)[self.yourCollectionView cellForItemAtIndexPath:indexPath];

if (cell.isSelected) {
    NSArray *indexes = [[NSArray alloc] init];
    [indexes arrayByAddingObject:indexPath];
    [self.yourCollectionView reloadItemsAtIndexPaths:indexes];
 }
}

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

NSLog(@"size for Item called");

CGSize size = self.view.frame.size;

__weak MainCollectionViewCell *cell = (MainCollectionViewCell*)[self.yourCollectionView cellForItemAtIndexPath:indexPath];

if (cell.isSelected) { 
    return CGSizeMake(size.width, size.height);
} else {
 // return your original item size
    return CGSizeMake(300, 300);
}
}