如何仅修改从collectionView中选择的项目的大小和位置?

时间:2015-03-10 10:23:33

标签: ios objective-c uicollectionview

我有一个集合视图,当我选择一个项目时,我想要更大的图像并添加一个框架。我这样做,但其余的图像也在移动。这就是我的表现:

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

    if ([selectedIndexPath isEqual:indexPath]) {
       selectedIndexPath = nil;
        [self.player stop];

        CollectionViewCell *cellOne =  [collectionView cellForItemAtIndexPath:indexPath];
        cellOne.inner.hidden=YES;


        recipeImageView.frame=CGRectMake(recipeImageView.frame.origin.x, recipeImageView.frame.origin.y, 100, 100);
        recipeImageView.layer.borderColor = [[UIColor blackColor] CGColor];

    }
    else {
        // select new cell
        celulaSelectata=indexPath;
        UICollectionViewCell  *cell = [collectionView cellForItemAtIndexPath:indexPath];
        recipeImageView = (UIImageView *)[cell viewWithTag:100];

        CollectionViewCell *cellOne =  [collectionView cellForItemAtIndexPath:indexPath];

        recipeImageView.frame=CGRectMake(recipeImageView.frame.origin.x, recipeImageView.frame.origin.y, 120, 120);
        cellOne.inner.frame=recipeImageView.frame;

如何仅修改所选项目的大小和位置?

2 个答案:

答案 0 :(得分:1)

selectedIndexPath为所选单元格创建UICollectionViewLayoutAttributes并致电invalidateItemsAtIndexPaths:(NSArray *)indexPaths更新所选UICollectionViewLayoutAttributes的单元格

对您的UICollectionViewLayout进行子类化并在UICollectionViewLayout中添加功能,并使任何indexPath无效以更新

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attribute = [super layoutAttributesForItemAtIndexPath:indexPath];
    if ([indexPath isEqual:selectedIndexPath]) {
        //set attributes
        //attribute.frame = //setframe;
    }
    return attribute;
}

答案 1 :(得分:0)

if ([selectedIndexPath isEqual:indexPath])

我认为您必须在cellforRow方法中编写此部分。

不在didSelectRowAtIndexpath