我可以在收藏夹视图中选择图像。选择图像时,选择了错误的图像单元。一旦我向下滚动到该单元格之外,则不再选择该单元格。如何解决此问题?
imageView在情节提要中定义。资产在照片库中。
这是PhotoCell.h文件。
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
@interface PhotoCell : UICollectionViewCell
@property(nonatomic,strong) ALAsset * asset;
@property (nonatomic,weak) IBOutlet UIImageView * PhotoImageView;
这是我的PhotoCell.m文件。
#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];
}
您不能在中添加复选框,也不能在中didSelectItemAtIndexPath
删除复选框didDeselectItemAtIndexPath
,因为滚动时所有单元格都将被重用。
在PhotoCell
和cellForItemAtIndexPath
功能中添加复选框,执行以下操作:
if cell.selected {
checkbox.hidden = false
}
else {
checkbox.hidden = true
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句