Refreshing a UICollectionview

前端 未结 3 2138
陌清茗
陌清茗 2021-02-18 14:53

Does anyone know how to reload/refresh a UICollectionView while the collection view is being displayed? Basically I\'m looking for something similar to the standard reloadData m

3条回答
  •  囚心锁ツ
    2021-02-18 15:24

    The correct and best way is to use

    NSMutableArray *indexPaths = [NSMutableArray array];
    //prepare some data
    //batchupdate as block
    [self.collectionView performBatchUpdates:^{
        //operations like delete
       [self.collectionView deleteSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, count)]];
        [self.collectionView insertItemsAtIndexPaths:indexPaths];
    } completion:^(BOOL finished) {
        // call something when ready
        }
    }];
    

    and all gets precomputed an nicely animated. The best practice is to first remove and than add all elements, to avoid conflicts.

提交回复
热议问题