4

セルを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];
}

明らかな何かが欠けているように感じますが、これは私を困惑させました。

4

2 に答える 2

0

これは私にとってはうまくいきます、質問の議論から引用

[self.dataArray removeObjectAtIndex:index];
[self.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
[self.collectionView performBatchUpdates:^{
    [self.collectionView reloadData];
} completion:nil];
于 2018-04-27T05:50:28.143 に答える