ios UICollectionView 更新单个单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20296929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:34:28  来源:igfitidea点击:

UICollectionView update a single cell

iosobjective-cuitableviewuicollectionviewuicollectionviewcell

提问by Paulius Dragunas

I am attempting to update a single cell inside a UICollectionView, specifically I am just trying to update an image in that specific cell detonated by cell.imageView. [self.collectionView reloadData]is not really an option because it makes the whole collection view flash. [self.collectionView beginUpdates]is not a thing in a UICollectionViewit seems.

我正在尝试更新 . 中的单个单元格UICollectionView,特别是我只是尝试更新由cell.imageView. [self.collectionView reloadData]不是一个真正的选择,因为它使整个集合视图闪烁。似乎[self.collectionView beginUpdates]不是一个东西UICollectionView

I understand I may be able to use:

我知道我可以使用:

[self.collectionView performBatchUpdates:^{
    //do something
} completion:nil];

I am not exactly sure what to put inside a completion block to update that certain cell's imageView. This is all being done inside of didSelectItemAtIndexPath.Also I am not using NSFetchedResultsController. Any ideas?

我不确定在完成块中放什么来更新特定单元格的imageView. 这一切都是在didSelectItemAtIndexPath.Also内部完成的,我也没有使用NSFetchedResultsController. 有任何想法吗?

回答by Dinesh Raja

- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths

- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths

Here it is a method to reload the specific indexPathsin your collectionView

这是重新加载特定的方法indexPaths在你的collectionView

回答by gavdotnet

Dinesh's answer is spot-on. But to avoid unwanted animations when reloading (aka "flashing") use:

Dinesh 的回答是准确的。但是为了避免在重新加载(又名“闪烁”)时出现不需要的动画,请使用:

BOOL animationsEnabled = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
[myCollectionView reloadItemsAtIndexPaths:myIndexPaths];
[UIView setAnimationsEnabled:animationsEnabled];

回答by Vizllx

See Inserting, Deleting, and Moving Sections and Itemsfrom the "Collection View Programming Guide for iOS":

请参阅“iOS 集合视图编程指南”中的插入、删除和移动部分和项目

To insert, delete, or move a single section or item, you must follow these steps:

要插入、删除或移动单个部分或项目,您必须执行以下步骤:



Update the data in your data source object. Call the appropriate method of the collection view to insert or delete the section or item. It is critical that you update your data source before notifying the collection view of any changes. The collection view methods assume that your data source contains the currently correct data. If it does not, the collection view might receive the wrong set of items from your data source or ask for items that are not there and crash your app.

更新数据源对象中的数据。调用集合视图的适当方法来插入或删除部分或项目。在通知集合视图任何更改之前更新数据源至关重要。集合视图方法假定您的数据源包含当前正确的数据。如果没有,集合视图可能会从您的数据源接收错误的项目集,或者请求不存在的项目并导致您的应用程序崩溃。



So in your case, you must add an image to the collection view data source first and then call insertItemsAtIndexPaths. The collection view will then ask the data source delegate function to provide the view for the inserted item.

因此,在您的情况下,您必须先将图像添加到集合视图数据源,然后再调用 insertItemsAtIndexPaths。然后集合视图将要求数据源委托函数为插入的项目提供视图。