滚动时,单元格未正确显示

时间:2015-06-13 13:45:52

标签: ios

我在集合视图中有可以选择的图像。当我选择图像时,选择了错误的图像单元格。一旦我向下滚出单元格的视图,然后备份不再选择单元格。我该如何解决这个问题?

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

}

2 个答案:

答案 0 :(得分:1)

您无法在didSelectItemAtIndexPath中添加复选框并将其移除didDeselectItemAtIndexPath,因为滚动时将重复使用所有单元格。

PhotoCellcellForItemAtIndexPath功能中添加复选框,执行以下操作:

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

答案 1 :(得分:0)

您应该知道dequeueReusableCellWithIdentifier如何与tableview单元一起使用。当您致电[tableView dequeueReusableCellWithIdentifier:]时,它将获得之前已创建且目前尚未使用的单元格。这意味着在需要时可以重用相同的UITableCell对象。

因此,当您在didSelectItemAtIndexPath委托中添加复选框按钮并向上/向下滚动时,此单元格将重新使用另一个单元格索引,因此单元格显示为已选中。

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

最后,重置[删除复选框&amp;如果索引路径包含在所选索引路径数组中,则在cellForItemAtIndexPath数据源处单元格,然后将单元格标记为已选中。

请参阅以下代码作为尚未测试的示例。

在故事板中添加一个按钮,然后按如下方式输出:

@interface PhotoCell : UICollectionViewCell

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

@end

更新PhotoCell如下:

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

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

希望它能奏效。

感谢。