在集合视图中滚动时选择了多个单元格 [英] Multiple Cells selected on scrolling in collection view

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

问题描述

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

I am facing following problems in collection view :
Problem 1. Whenever I select an item in the top cells and scroll to bottom, another item at bottom is also selected :


滚动后


After Scrolling

问题2:现在在第一个场景之后,如果我向上滚动并选择另一个单元格,那么之前选择的单元格仍然不会改变背景.

Problem 2 : Now after the first scenario, if I scroll up and select another cell, then the previously selected cell still does not change the background.

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

请让我知道我哪里出错了,是改变颜色的逻辑,还是我遗漏了什么.

Please let me know where I am going wrong, is the logic of changing colors, or I am missing something.

推荐答案

因为单元格被重复使用,这就是它产生问题的原因.最好这样使用:

Because the cells are being reused that's why it's making a problem. Better use like this:

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

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

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