iosuicollectionviewuicollectionviewcelluicollectionviewdelegate

cellForItemAtIndexPath get the wrong value of indexPath in block


I create a button in a custom UICollectionViewCell, and add a target in the button block like:

@interface MYCollectionViewCell : UICollectionViewCell
@property (nonatomic, copy) void (^clickButtonBlock)(BOOL boolValue);
@end

and I set the block to delete the cell I click the button in the cellForItemAtIndexPath, like:

[self.collectionView performBatchUpdates:^{
    [strongSelf.dataArray removeObjectAtIndex:index];
    [strongSelf.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]]];
    [strongSelf.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:5 inSection:0]]];
}];

after I delete the first cell. The next cell's NSIndexPath was wrong, And the function [collectionView indexPathForCell:strongCell]; in the block get the correct Value.

I don't know why they are different?


Solution

  • You are not calling collectionView.reloadData() after deletion, and hence you don't get the correct indexpath after delete.

    Please call the method and then check.