セルをUICollectionView
追加および削除できる があります。を使用してこれらの変更を行ってperformBatchUpdates
いますが、レイアウトは期待どおりにアニメーション化されています。
コンテンツの最後までスクロールしてアイテムを削除すると、問題が発生contentSize
します。これにより、contentOffset
が変更されますが、変更はアニメーション化されません。代わりにcontentOffset
、削除アニメーションが完了した直後にジャンプします。contentOffset
削除と並行して手動で更新しようとしましたが、それもうまくいきませんでした。
カスタム レイアウトを使用していますが、次のコードを使用すると、標準のフロー レイアウトでも同じ動作が見られます。
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.items.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = [self.items objectAtIndex:indexPath.item];
return cell;
}
- (IBAction)addItem:(UIBarButtonItem *)sender
{
self.runningCount++;
[self.items addObject:[NSString stringWithFormat:@"Item %u",self.runningCount]];
[self.collectionView performBatchUpdates:^{
[self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:self.items.count-1 inSection:0]]];
} completion:nil];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self.items removeObjectAtIndex:indexPath.item];
[self.collectionView performBatchUpdates:^{
[self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
} completion:nil];
}
明らかな何かが欠けているように感じますが、これは私を困惑させました。