2017-06-01 20 views
0

目前我有我的didselect擴大我的每一個細胞這樣的選擇上UICollectionView細胞:如何擴大時不會弄亂集合視圖

@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) 

} 

我遇到的問題是,細胞開始相互重疊而不是向下調整到大單元大小。我希望能夠點擊任何單元格,它會展開爲更大的尺寸,而其他單元格會向下移動以應對此調整大小。點擊第二個單元格會使第一個單元格更小。我如何去做這件事?它是一場鬥爭!

+0

請HALP ....... –

回答

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); 
} 
}