2

我在uicollectionview单元格中填充数据并选择和取消选择,一切都很完美,但是当我开始滚动时,选择不是有时候会随着单元格选择而改变。以下是代码,非常感谢。UICollectionview在滚动时更改选择/禁用选择 - iOS

- (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.

+0

你的代码缩进很少。由于理解你的代码现在需要很多时间,所以它很难帮助你。 – Shubhank

回答

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

注意: -类名始终以大写字母开头,所以如果您更改类名称pizzaInfoPizzaInfo,它的良好编码准则的建议,它是连击。

+0

非常感谢你的回答。但它仍然在滚动选择正在进行。 [self._byodelegate deltaDeSelection:pizzaInfoCellData]/[self._byodelegate deltaSelection:pizzaInfoCellData];用这些代码行从数组中删除/添加对象,这就是为什么要使用if else块。你能再请检查一下吗? –

+0

@Mac_Play检查我编辑的答案。 –