gpt4 book ai didi

ios - 如何在选择时展开 UICollectionView 单元格而不弄乱 Collection View

转载 作者:行者123 更新时间:2023-11-30 12:25:12 25 4
gpt4 key购买 nike

目前我有一个 didselect 来扩展我的每个单元格,如下所示:

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

}

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

最佳答案

您应该在 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);
}
}

关于ios - 如何在选择时展开 UICollectionView 单元格而不弄乱 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44297073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com