Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 刷新UICollectionview_Ios_Objective C_Uicollectionview_Reloaddata - Fatal编程技术网

Ios 刷新UICollectionview

Ios 刷新UICollectionview,ios,objective-c,uicollectionview,reloaddata,Ios,Objective C,Uicollectionview,Reloaddata,是否有人知道在显示集合视图时如何重新加载/刷新UICollectionView?基本上,我正在寻找与UITableview的标准重载数据方法类似的方法。您可以调用: [self.myCollectionView reloadData]; 也可以重新加载各个部分和项目: [self.myCollectionView reloadSections:indexSet]; [self.myCollectionView reloadItemsAtIndexPaths:arrayOfIndexPaths]

是否有人知道在显示集合视图时如何重新加载/刷新UICollectionView?基本上,我正在寻找与UITableview的标准重载数据方法类似的方法。

您可以调用:

[self.myCollectionView reloadData];
也可以重新加载各个部分和项目:

[self.myCollectionView reloadSections:indexSet];
[self.myCollectionView reloadItemsAtIndexPaths:arrayOfIndexPaths];

正确和最好的方法是使用

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
    }
}];

所有这些都被预先计算成一个很好的动画。最好的做法是先删除然后再添加所有元素,以避免冲突。

也许我有点误解,但UICollectionView实现了重载数据。WFIW我相信人们之所以会遇到这种情况,是因为他们习惯于调用[myTableView reloadData],但对于UICollectionView,您必须调用[myCollectionView.collectionView重载数据][self.myCollectionView重载数据];只调用一次。要重新加载所有数据。虽然这确实会达到预期的结果,但使用重新加载数据会重新绘制所有单元格,并可能影响性能。这不是一个好主意,因为重新加载整个表会导致性能变差,并且会消耗大量精力。您节省了我的时间!谢谢
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
    }
}];