2015-06-13 74 views
-1

我可以选择一个集合视图中的图像当我选择一个图像时,选择了错误的图像单元格一旦向下滚动出单元格视图,备份的细胞不再被选中,我怎样才能解决这个问题?滚动时单元格显示不正确

的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]; 

} 
+1

你可以在你的问题上花更多时间,并尝试更好地解释问题吗?否则,你不会得到任何吻... – tailec

+0

它可能有可能,我需要花一些时间在我的问题,但PLZ试图理解。 –

回答

1

didSelectItemAtIndexPath您不能添加的复选框,并在didDeselectItemAtIndexPath,删除它,因为所有的细胞都会滚动时被重用。

添加复选框,在您的PhotoCell,并在cellForItemAtIndexPath功能,这样做:

if cell.selected { 
    checkbox.hidden = false 
} 
else { 
    checkbox.hidden = true 
} 
+0

Jacky我不知道该怎么办。您能否详细解释 –

+0

@vikramsingh在'PhotoCell'中显示您的代码。 – Jacky

+0

我是一个新的iPhone很抱歉问你,但PLZ -Jacky,我在故事板中创建了单元格,所以我需要在故事板或cellforItemAtIndexPath中添加chekbox - –

0

你应该知道如何dequeueReusableCellWithIdentifier工作与实现代码如下细胞。当您拨打[tableView dequeueReusableCellWithIdentifier:]时,它将获得一个先前已创建并且当前未被使用的单元。这意味着在需要时可以重复使用相同的UITableCell对象。

因此,当您添加复选框按钮didSelectItemAtIndexPath委托并滚动到底部/向上时,此单元格将重新用于其他单元格索引,并因此显示单元格被选中。

要解决此问题,您需要将选定的索引路径存储在didSelectItemAtIndexPath处,并在didDeselectItemAtIndexPath方法中删除。

最后,重置[删除复选框&拇指]单元格cellForItemAtIndexPath数据源,然后将单元格标记为选中,如果索引路径包含在选定的索引路径数组中。

查看下面的代码,作为尚未测试过的示例。

从故事板添加一个按钮,并出口如下:

@interface PhotoCell : UICollectionViewCell 

@property(nonatomic,strong) ALAsset * asset; 
@property (nonatomic,weak) IBOutlet UIImageView * PhotoImageView; 
@property (nonatomic,weak) IBOutlet UIButton * chkboxBtn; 

@end 

更新光电如下:

#import "PhotoCell.h" 

@interface PhotoCell() 

- (void)setCheckBoxSelected:(BOOL)selected 

@end 

@implementation PhotoCell 

#pragma mark - User Made Method 
- (void) setAsset:(ALAsset *)asset 
{ 
    // 2 
    _asset = asset; 
    self.PhotoImageView.image = [UIImage imageWithCGImage:[asset thumbnail]]; 
} 
- (void)setCheckBoxSelected:(BOOL)selected 
{ 
    if (selected) { 
     [self.chkboxBtn setImage:[UIImage imageNamed:@"success.png"] forState:UIControlStateNormal]; 
    } else { 
     [self.chkboxBtn setImage:nil forState:UIControlStateNormal]; 
    } 
} 
@end 

最后修改控制器如下 在你的控制器类添加的NSMutableArray属性

@property(nonatomic, strong) NSMutableArray *selectedIndexPaths; 

初始化数组at viewDidLoad

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.selectedIndexPaths = [NSMutableArray new]; 
} 

然后..

#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]; 

    if ([self.selectedIndexPaths containsObject:indexPath]) { 
     [cell setCheckBoxSelected:NO]; 
    } else { 
     [cell setCheckBoxSelected:YES]; 
    } 
} 

#pragma mark - Collection View Delegate 

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 

    [self.selectedIndexPaths addObject:indexPath]; 

    PhotoCell *cell = (PhotoCell *)[collectionView cellForItemAtIndexPath:indexPath]; 
    [cell setCheckBoxSelected:YES]; 
} 

-(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 setCheckBoxSelected:NO]; 
    [self.selectedIndexPaths removeObject:indexPath]; 
} 

@end 

希望它会工作。

谢谢。

相关问题