【问题标题】:UICollectionview changes selection/disabling selection while scrolling - iOSUICollectionview 在滚动时更改选择/禁用选择 - iOS
【发布时间】:2026-01-12 06:30:02
【问题描述】:

我在 uicollectionview 单元格上填充数据并选择和取消选择,一切正常,但是当我开始滚动时,有时选择不存在,有时选择随单元格而变化。以下是代码,不胜感激。

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

      cell = (BYOCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CVCCell" forIndexPath:indexPath];
      cell.vSelectionView.hidden = YES;
      cell.vSelectionView.backgroundColor = customLightGreenColor;
      [self makeRoundElement:cell.vSelectionView forLabel:nil withCorner:8.0f withBorder:0];

      pizzaInfo *pizzainfo= [[pizzaInfo alloc]init];
      pizzainfo = [_lstDishCollection objectAtIndex: indexPath.row];  
      if (pizzainfo._bIsSelected)
      {
           cell.vSelectionView.hidden = NO;
      }
      else
      {
           cell.vSelectionView.hidden = YES;
      }
      //label customization 
      return cell;
 }

DidselectItem

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
      cell = (BYOCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CVCCell" forIndexPath:indexPath];
      pizzaInfo *pizzaInfoCellData = [_lstDishCollection objectAtIndex: indexPath.row];
      byoPizzaInfo = [_lstDishCollection objectAtIndex:indexPath.row];
      if ( pizzaInfoCellData._bIsSelected)
      {       
           cell.vSelectionView.hidden = NO;
           pizzaInfoCellData._bIsSelected = NO;
           [self._byodelegate deltaDeSelection:pizzaInfoCellData];
      }
      else
      {
           cell.vSelectionView.hidden = YES;
           pizzaInfoCellData._bIsSelected = YES;
           // deltaSelection:(pizzaInfo *)selectedItem
           [self._byodelegate deltaSelection:pizzaInfoCellData];
           if (self._IsNotifiable) {            
                [self showView];            
           }
      }
      [_vCVC reloadData];
 }

collectionViewCell 的更多内容在 tableViewCell. 内部

【问题讨论】:

  • 您的代码缩进很差。由于现在理解您的代码将花费大量时间,因此很难为您提供帮助。

标签: ios uitableview uicollectionview boolean uicollectionviewlayout


【解决方案1】:

在您的cellForItemAtIndexPath 中,您已经添加了隐藏和显示所选视图的条件,因此您需要像这样更改您的didSelectItemAtIndexPath

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
     pizzaInfo *pizzaInfoCellData = [_lstDishCollection objectAtIndex: indexPath.row];
     if (pizzaInfoCellData._bIsSelected)
     {       
          [self._byodelegate deltaDeSelection:pizzaInfoCellData];
     }
     else
     {
          [self._byodelegate deltaSelection:pizzaInfoCellData];
     }
     pizzaInfoCellData._bIsSelected = !pizzaInfoCellData._bIsSelected
     [_vCVC reloadData];
}

注意:- 类名总是以大写字母开头,因此如果您将类名pizzaInfo 更改为PizzaInfo,它会为您提供良好的编码指南建议。

【讨论】:

  • 非常感谢您的回答。但它仍然在滚动选择关闭时。 [self._byodelegate deltaDeSelection:pizzaInfoCellData] / [self._byodelegate deltaSelection:pizzaInfoCellData];使用这些代码行从数组中删除/添加对象,这就是我使用 if else 块的原因。你能再检查一下吗?