I have images within a collection view that I can select. When I select an image(s) the wrong image cells is selected. Once I scroll down out of view of the cell then back up the cell is no longer selected. How can I fix this issue?
The imageView is defined in the storyboard. The assets are in the photo library.
This is the PhotoCell.h file.
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
@interface PhotoCell : UICollectionViewCell
@property(nonatomic,strong) ALAsset * asset;
@property (nonatomic,weak) IBOutlet UIImageView * PhotoImageView;
This is my PhotoCell.m file.
#import "PhotoCell.h"
@interface PhotoCell ()
@end
@implementation PhotoCell
#pragma mark - User Made Method
- (void) setAsset:(ALAsset *)asset
{
// 2
_asset = asset;
self.PhotoImageView.image = [UIImage imageWithCGImage:[asset thumbnail]];
}
#pragma mark - CollectionView Cell Method
-(void)prepareForReuse
{
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PhotoCell *cell =(PhotoCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath];
ALAsset * asset = self.assets[indexPath.row];
cell.asset = asset;
cell.backgroundColor = [UIColor redColor];
}
#pragma mark - Collection View Delegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//NSLog(@"%@ - %d", NSStringFromSelector(_cmd), indexPath.item);
PhotoCell *cell = (PhotoCell *)[collectionView cellForItemAtIndexPath:indexPath];
chkboxBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[chkboxBtn setFrame:CGRectMake(60, 60, 30, 30)];
[chkboxBtn setTag:100];
[chkboxBtn setImage:[UIImage imageNamed:@"success.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:chkboxBtn ];
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
// This removes the Check Box Button From the Cell After click it again
PhotoCell *cell =(PhotoCell *)[collectionView cellForItemAtIndexPath:indexPath];
[[cell.contentView viewWithTag:100] removeFromSuperview];
}