【问题标题】:How to expand UICollectionView Cell on when selected without messing up the collection view如何在选择时展开 UICollectionView Cell 而不会弄乱集合视图
【发布时间】:2017-06-01 01:26:51
【问题描述】:

目前我有一个我的 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)

}

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

【问题讨论】:

  • 请停下来......

标签: ios swift uicollectionview cell


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多