UICollectionview在滚动时更改选择/禁用选择 - iOS

时间:2016-07-17 08:59:48

标签: ios uitableview uicollectionview boolean uicollectionviewlayout

我在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.以上的内容已超过mDrawerLayout.bringToFront(); mDrawerLayout.requestLayout();

1 个答案:

答案 0 :(得分:2)

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,则会更好,这是对良好编码指南的建议。< / p>