简体   繁体   English

UICollectionView更新单个单元格

[英]UICollectionView update a single cell

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 . 我正在尝试更新UICollectionView的单个单元格,特别是我只是尝试更新由cell.imageView引爆的特定单元格中的图像。 [self.collectionView reloadData] is not really an option because it makes the whole collection view flash. [self.collectionView reloadData]实际上不是一个选项,因为它使整个集合视图闪烁。 [self.collectionView beginUpdates] is not a thing in a UICollectionView it seems. [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 . 我不确定在完成块中放置什么来更新某个单元格的imageView This is all being done inside of didSelectItemAtIndexPath .Also I am not using NSFetchedResultsController . 这都是在didSelectItemAtIndexPath中完成的。我也没有使用NSFetchedResultsController Any ideas? 有任何想法吗?

- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths

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

Dinesh's answer is spot-on. Dinesh的回答是现实的。 But to avoid unwanted animations when reloading (aka "flashing") use: 但是为了避免在重新加载(也称为“闪烁”)时出现不需要的动画,请使用:

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

See Inserting, Deleting, and Moving Sections and Items from 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. 因此,在您的情况下,您必须首先将图像添加到集合视图数据源,然后调用insertItemsAtIndexPaths。 The collection view will then ask the data source delegate function to provide the view for the inserted item. 然后,集合视图将要求数据源委托函数提供插入项的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM