我可以選擇一個集合視圖中的圖像當我選擇一個圖像時,選擇了錯誤的圖像單元格一旦向下滾動出單元格視圖,備份的細胞不再被選中,我怎樣才能解決這個問題?滾動時單元格顯示不正確
的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];
}
你可以在你的問題上花更多時間,並嘗試更好地解釋問題嗎?否則,你不會得到任何吻... – tailec
它可能有可能,我需要花一些時間在我的問題,但PLZ試圖理解。 –