使用UICollectionView进行奇怪的动画行为(插入和删除项目)

Sam*_*Sam 8 iphone uitableview ios ios6 uicollectionview

从UICollectionView插入或删除项目时,似乎在动画期间会出现一个额外的单元格,并且此额外单元格向错误的方向移动.我已经尝试使用UITableView完全相同,并且没有问题.

该问题视频如下:https://dl.dropbox.com/u/11523469/CollectionViewBug.mov,左侧是集合视图,右侧是表视图.每个单元格中的数字是创建单元格时单元格的indexPath.item值.

问题首先在0:08和0:12(插入)之间的视频中,然后在0:16到0:20(删除)之间再次出现.

项目可在此处获取:https://dl.dropbox.com/u/11523469/CollectionViewBug.zip

即,当插入细胞时,插入细胞的下方的所有细胞向下移动以为新细胞腾出空间.但是这个额外的细胞出现并与其他细胞重叠并向上移动.

同样地,当删除单元格时,被删除的单元格下方的所有单元格向上移动以填充该单元格曾经存在的间隙.但是这个额外的细胞出现并与其他细胞重叠并向下移动.

要对集合视图执行的第一个操作(插入或删除)不会导致此问题.但是在随后的所有行动中,问题都存在.

有没有其他人遇到过与UICollectionView相同的问题?有没有人有解决方案或解决方法?

谢谢!

Gar*_*eth 10

我提出了一个似乎可以解决问题的解决方法,但是对于提供的示例非常具体.我的猜测是,当细胞被重复使用时,它们会有错误的起点导致奇怪的动画.

我更改了Storyboard以使用UICollectionViewFlowLayout的子类:

// MyFlowLayout - subclass of UICollectionViewFlowLayout

#import "MyFlowLayout.h"

@interface MyFlowLayout ()

@property (strong) NSMutableArray *deleteIndexPaths;
@property (strong) NSMutableArray *insertIndexPaths;
@property (assign) float rowOffset;

@end

@implementation MyFlowLayout

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        // minimumInteritemSpacing may be adjusted upwards but this example ignores that
        self.rowOffset = self.itemSize.height + self.minimumInteritemSpacing;
    }

    return self;
}

// As per Mark Pospesel corrections to CircleLayout

- (void)prepareForCollectionViewUpdates:(NSArray *)updateItems
{
    // Keep track of insert and delete index paths
    [super prepareForCollectionViewUpdates:updateItems];

    self.deleteIndexPaths = [NSMutableArray array];
    self.insertIndexPaths = [NSMutableArray array];

    for (UICollectionViewUpdateItem *update in updateItems)
    {
        if (update.updateAction == UICollectionUpdateActionDelete)
        {
            [self.deleteIndexPaths addObject:update.indexPathBeforeUpdate];
        }
        else if (update.updateAction == UICollectionUpdateActionInsert)
        {
            [self.insertIndexPaths addObject:update.indexPathAfterUpdate];
        }
    }
}

- (void)finalizeCollectionViewUpdates
{
    [super finalizeCollectionViewUpdates];
    // release the insert and delete index paths
    self.deleteIndexPaths = nil;
    self.insertIndexPaths = nil;
}

// The next two methods have misleading names as they get called for all visible cells on     both insert and delete

- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
    // Must call super
    UICollectionViewLayoutAttributes *attributes = [super initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];
    if (!attributes)
        attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];

    if ([self.insertIndexPaths containsObject:itemIndexPath]) {
        // Initial position for an inserted cell is it's final position - fades in
        CGRect frame = attributes.frame;
        frame.origin.y = itemIndexPath.row * self.rowOffset;
        attributes.frame = frame;
        attributes.zIndex = -1; // stop the inserted cell bleeding through too early in the animation
    }
    if ([self.deleteIndexPaths count]) {
        NSIndexPath *deletedPath = self.deleteIndexPaths[0];  // Might be more than one but this example ignores that
        if (itemIndexPath.row > deletedPath.row) {
            // Anything after the deleted cell needs to slide up from the position below it's final position
            // Anything before the deleted cell doesn't need adjusting
            CGRect frame = attributes.frame;
            frame.origin.y = ((itemIndexPath.row + 1) * self.rowOffset);
            attributes.frame = frame;
        }
    }

    return attributes;
}

- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
    UICollectionViewLayoutAttributes *attributes = [super finalLayoutAttributesForDisappearingItemAtIndexPath:itemIndexPath];
    if (!attributes)
        attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];

    // I would have expected the final positions to already be correct but my guess is that re-used cells
    // are not considered until after the animation block settings have been generated
    CGRect frame = attributes.frame;
    frame.origin.y = itemIndexPath.row * self.rowOffset;
    attributes.frame = frame;

    if ([self.deleteIndexPaths containsObject:itemIndexPath]) {
        // Fade out the deleted cell
        attributes.alpha = 0.0;
    }

    return attributes;
}

@end
Run Code Online (Sandbox Code Playgroud)