UICollectionView未正确调整大小

时间:2015-01-11 00:30:21

标签: ios objective-c uicollectionview uicollectionviewcell

尝试在用户按下UICollectionViewCell时调整其大小。此刻,细胞大小调整,但其他细胞不会对其作出反应。以下是该问题的图片:

enter image description here

这是我正在使用的代码。我觉得问题在于无效,但我不确定:

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


    UICollectionViewCell * cellToChangeSize = [self.collectionView cellForItemAtIndexPath:indexPath]; // Avoid retain cycles


    void (^animateChangeWidth)() = ^()
    {
        CGRect frame = cellToChangeSize.frame;
        frame.size = CGSizeMake(300, 100);
        cellToChangeSize.frame = frame;
    };

    // Animate

    [UIView transitionWithView:cellToChangeSize duration:0.1f options: UIViewAnimationOptionCurveLinear animations:animateChangeWidth completion:^(BOOL finished) {
        [self.collectionView.collectionViewLayout invalidateLayout];


    }];

}

1 个答案:

答案 0 :(得分:0)

您可以通过扩展 UICollectionViewDelegateFlowLayout 并实现 collectionView:layout:sizeForItemAtIndexPath:方法来实现:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UICollectionViewDelegateFlowLayout_protocol/index.html#//apple_ref/occ/intfm/UICollectionViewDelegateFlowLayout/collectionView:layout:sizeForItemAtIndexPath :.

我没有直接在 collectionView:didSelectItemAtIndexPath:中修改单元格,而是在模型类中设置大小,您需要在流程布局中使用它子类。

希望这有帮助。

相关问题