gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:44:19 24 4
gpt4 key购买 nike

我有一个收藏 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com