How to reload the same indexPath item for UICollectionView?

Hi all,


When I use '[collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath]' in fuction:

'- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath',

I find item with the same indexPath by 'dequeueReusableCellWithReuseIdentifier' is not the same item from last reloadData.


For example, when UICollectionView reloadData,the item for indexPath 0-0 by 'dequeueReusableCellWithReuseIdentifier' will return the item which is the indexPath 0-4 at last reloadData.


I test many times and find that if UICollectionView have many items with same reuseIdentifier, the function 'dequeueReusableCellWithReuseIdentifier:forIndexPath:' will return the item after UICollectionView reloadData in these items with same reuseIdentifier randomly.


It seems different between UICollectionView and UITableView.UITableView will return the same indexPath cell after reloadData when dequeue cell.


Can you tell me how to reload the same item for UICollectionView?


Thanks,

Jason Xu

Replies

That would defeat the purpose of having reusable cells.


What are you trying to accomplish?

You should not care which item you return "internlly". What is important is to return the item at the given position.


If you have trouble here, taht's probably because you have some design flaw elsewhere. For instance, if you try to use collectionView as a dataStore, which you should never do.


Please post the complete class code for more specific help.

I have a problem that the imageView in the cell will load another url image,so the imageView will flash.


For Example:


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

TestCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TestCollectionViewCell" forIndexPath:indexPath];

TestImageView *imageView = [cell getImageView];

imageView.url = self.data[indexPath.row];

return cell;

}


TestImageView have the property url to download the url image.(like SDWebImage)

If `dequeueReusableCellWithReuseIdentifier` return another cell, the url will be diffrent and imageView will replace the current image with the url image.So the user will find the cell will splash one time if uicollectionview reloadData.


If `dequeueReusableCellWithReuseIdentifier` can return the same cell, the url property will be equal and imageView will not replace current image.


That's my problem.Thanks

Thanks for your reply.


I have a problem that the imageView in the cell will load another url image,so the imageView will flash.


For Example:


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

TestCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TestCollectionViewCell" forIndexPath:indexPath];

TestImageView *imageView = [cell getImageView];

imageView.url = self.data[indexPath.row];

return cell;

}


TestImageView have the property url to download the url image.(like SDWebImage)

If `dequeueReusableCellWithReuseIdentifier` return another cell, the url will be diffrent and imageView will replace the current image with the url image.So the user will find the cell will splash one time if uicollectionview reloadData.


If `dequeueReusableCellWithReuseIdentifier` can return the same cell, the url property will be equal and imageView will not replace current image.


That's my problem.Thanks

What is

    TestImageView *imageView = [cell getImageView];

doing ?


Why do you need it ?

OK, then I think the proper thing to do would be to not reload the cells whose images aren’t changing. Don’t call reloadData(). Insert/delete/reload only the cells that are changing.