集合视图单元格多项选择错误 [英] Collection View Cell multiple item select Error

查看:26
本文介绍了集合视图单元格多项选择错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个收藏视图 &我想选择多个项目.为此,我正在使用

I have a Collection View & I want to select more than one item. For that I'm using

- (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.

This method to add objects to 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;
}

我使用此代码来改变单元格的背景颜色.

I use this code to chance the background colour of the cell.

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

但是当我在集合视图中选择第 1 个项目时,第一个和集合视图中的第 15 项更改背景颜色.

But when I select 1st item in the Collection View, both 1st & 15th item in the Collection View change the background Colour.

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

Why is this happen? Please can someone give me a solution.

推荐答案

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

Ok, here's what seems to be your problem.

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

1) When you select first cell this method is called

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

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

In this method you change cell background colour to be black, so this cell is black from now on.

2) 您向下滚动并使用方法加载新单元格

2) You scroll down and new cells are loaded with method

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

实现中有一个棘手的问题

There is one tricky line in implementation

dequeueReusableCellWithReuseIdentifier:

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

So for new cell your app will likely to not create new cell but to show one that is not visible, for example cell #1, which you selected at beginning, and which got black background colour.

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

So for new cell your app might reuse old one which might be modified.

我为你解决的问题是下一个 -

My fix for you would be next -

- (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;
}

这篇关于集合视图单元格多项选择错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆