ios - Collection View 单元格多项选择错误

标签 ios objective-c uicollectionview uicollectionviewcell

我有一个收藏 View ,我想选择多个项目。为此,我正在使用

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.selectedAsset addObject:self.assets[indexPath.row]];

    UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor blackColor];
}

此方法将对象添加到 selectedAsset NSMutableArray。

这是 cellForItemAtIndexPath 方法。

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{

    Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];

    // load the asset for this cell
    ALAsset *asset = self.assets[indexPath.row];
    CGImageRef thumbnailImageRef = [asset thumbnail];
    UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];

    // apply the image to the cell
    cell.imageView.image = thumbnail;
    [cell.label removeFromSuperview];
    //cell.imageView.contentMode = UIViewContentModeScaleToFill;

    return cell;
}

我使用这段代码来改变单元格的背景颜色。

UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor blackColor];

但是当我在 Collection View 中选择第一个项目时,Collection View 中的第一个和第 15 个项目都会改变背景颜色。

为什么会这样?请有人能给我一个解决方案。

最佳答案

好的,这似乎是您的问题。

1) 当您选择第一个单元格时调用此方法

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

在此方法中,您将单元格背景颜色更改为黑色,因此此单元格从现在开始为黑色。

2) 向下滚动,新单元格加载方法

    -(UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;

实现中有一个棘手的问题

dequeueReusableCellWithReuseIdentifier:

因此,对于新单元格,您的应用可能不会创建新单元格,而是显示一个不可见的单元格,例如您在开始时选择的单元格 #1,它的背景颜色为黑色。

因此,对于新单元格,您的应用可能会重用可能会修改的旧单元格。

接下来是我为您解决的问题 -

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{

UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];

//line below might not work, you have to tune it for your logic, this BOOL needs to return weather cell with indexPath is selected or not
BOOL isCellSelected = [self.selectedAsset containsObject:self.assets[indexPath.row]];
if(!isCellSelected)
{
    UICollectionViewCell* cell=[cv cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor clearColor]; // or whatever is default for your cells
}

// load the asset for this cell
ALAsset *asset = self.assets[indexPath.row];
CGImageRef thumbnailImageRef = [asset thumbnail];
UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];

// apply the image to the cell
cell.imageView.image = thumbnail;
[cell.label removeFromSuperview];
//cell.imageView.contentMode = UIViewContentModeScaleToFill;

return cell;
}

关于ios - Collection View 单元格多项选择错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22958393/

相关文章:

ios - 如何使用 NSManagedObject 的新 fetchRequest 函数发出获取请求?

ios - 代号一 - iOS 调试构建安装失败

ios - 目标选择器不适用于 Swift3

objective-c - 打开 NSOpenPanel 时插件 com.getdropbox.dropbox.garcon 中断

ios - 如何删除 Collection View 的背景

ios - 删除单元格时的 UICollectionView 动画

ios - 从多个 View Controller 输入数据时如何更新核心数据

iphone - 更改 UInavigationbar 标题后页

ios - 即使启用了位置服务,(PHAsset 位置)也会为相机胶卷照片返回(null)

iphone - 为什么 indexPathForItemAtPoint 总是返回 nil