在集合视图中滚动时选择了多个单元格

我在集合视图中面临以下问题:
问题1.每当我在顶部单元格中选择一个项目并滚动到底部时,底部的另一个项目也会被选中:

单元格选在顶部
滚动后
在此处输入图像描述

问题2:现在在第一个场景之后,如果我向上滚动并选择另一个单元格,则之前选择的单元格仍然不会更改背景。
在此处输入图像描述

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ //Cell from the prototype _appliancesViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AppliancesCell" forIndexPath:indexPath]; NSLog(@"IndexPath.row = %ld .item %ld",indexPath.row,indexPath.item); if(indexPath.row == self.selectedRow){ _appliancesViewCell.contentView.backgroundColor = UIColorFromRGB(0xd3d3d3); }else{ _appliancesViewCell.contentView.backgroundColor = [UIColor whiteColor]; } Appliance *appliances = [_appliancesArray objectAtIndex:indexPath.row]; _appliancesViewCell.applianceImage.image = appliances.applianceImage; _appliancesViewCell.applianceName.text = appliances.applianceName; _appliancesViewCell.applianceName.textColor =[UIColor textPrimaryColor]; _appliancesViewCell.layer.borderColor = UIColorFromRGB(0x9b9b9b).CGColor; _appliancesViewCell.layer.borderWidth = 1.0; _appliancesViewCell.layer.shadowRadius = 2.0; return _appliancesViewCell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *selectedCell = [collectionView cellForItemAtIndexPath:indexPath]; selectedCell.contentView.backgroundColor = UIColorFromRGB(0xd3d3d3); self.selectedRow = indexPath.row; Appliance *appliance_selected = _appliancesArray[indexPath.row]; _detailsArray = appliance_selected.descriptionsArray; } - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *selectedCell = [collectionView cellForItemAtIndexPath:indexPath]; selectedCell.contentView.backgroundColor = [UIColor whiteColor]; } 

请让我知道我哪里出错了,改变颜色的逻辑,或者我错过了什么。

因为细胞正在被重复使用,这就是它产生问题的原因。 更好地使用这样:

 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"AppliancesCell"; _appliancesViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; collectionView.allowsMultipleSelection = YES; if ([cell isSelected]) { _appliancesViewCell.contentView.backgroundColor = [UIColor blackColor]; }else { _appliancesViewCell.contentView.backgroundColor = [UIColor clearColor]; } return cell; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { _appliancesViewCell = (CategoryListCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; _appliancesViewCell.contentView.backgroundColor = [UIColor blackColor]; } - (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath { return YES; } -(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { _appliancesViewCell = (CategoryListCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; _appliancesViewCell.contentView.backgroundColor = [UIColor clearColor]; }