UICollectionview用户交互禁用第一个单元格,也会在滚动时禁用其他几个单元格

时间:2015-10-15 06:46:02

标签: ios objective-c uicollectionview

嗨我想在编辑模式下禁用uicollectionview的第一个单元格。我在cellforrow中执行了以下代码

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


   MainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    cell.curveimageView.image = nil;

   if(indexPath.item == 0){    
    [cell.curveimageView setImage:[UIImage imageNamed:@"AddCell"]];
     cell.subTitleText.text = @"New Cell";

       if(isEditing){
           [cell setUserInteractionEnabled:NO];
           [cell setAlpha:0.5];
       }
       else{
           [cell setUserInteractionEnabled:YES];
           [cell setAlpha:1];
       }
   }
   else{

        [cell.curveimageView setImage:[UIImage imageNamed:@"FlowerImage"]];
        cell.subTitleText.text = [NSString stringWithFormat:@"%ld%@",(long)indexPath.row,@"Hello is this looking ok"];
   }

   [cell setTag:indexPath.row];
    return cell;
}

第一次在编辑模式下,我能够让用户点击所有单元格但滚动后,第一行中的几个单元格被随机禁用。有人建议我解决这个问题的解决方案。

谢谢

4 个答案:

答案 0 :(得分:2)

在单元格上执行.home/vagrant/.bashrc时,使用该单元格创建的单元格也会受到影响。因为它们位于可以重用的队列中。因此,您需要在项目0控件的其他位置启用用户交互:

[cell setUserInteractionEnabled:NO];

答案 1 :(得分:1)

你的问题是:你的第一个单元格在滚动后重复使用,而你的代码不会改变它的状态,很快就可以解决这个问题:

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


  MainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

cell.curveimageView.image = nil;

if(indexPath.item == 0){    
[cell.curveimageView setImage:[UIImage imageNamed:@"AddCell"]];
 cell.subTitleText.text = @"New Cell";

   if(isEditing){
       [cell setUserInteractionEnabled:NO];
       [cell setAlpha:0.5];
   }
   else{
       [cell setUserInteractionEnabled:YES];
       [cell setAlpha:1];
   }
}
 else{

    [cell.curveimageView setImage:[UIImage imageNamed:@"FlowerImage"]];
    cell.subTitleText.text = [NSString stringWithFormat:@"%ld%@",(long)indexPath.row,@"Hello is this looking ok"];
// Add this:
[cell setUserInteractionEnabled:YES];
       [cell setAlpha:1];
 }

 [cell setTag:indexPath.row];
  return cell;
 }

答案 2 :(得分:1)

问题是重复使用单元格。在其他情况下设置setUserInteractionEnabled ...

else{

//  set user intraction enable .
[cell setUserInteractionEnabled:YES];

[cell.curveimageView setImage:[UIImage imageNamed:@"FlowerImage"]];
cell.subTitleText.text = [NSString stringWithFormat:@"%ld%@",(long)indexPath.row,@"Hello is this looking ok"];

   [cell setAlpha:1];
 }

希望对你有所帮助。

答案 3 :(得分:0)

在Swift 5中,我通过实现CollectionView的委托功能解决了这个问题

 func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
- get your cell where you want to disable user interaction
- if cell meet condition you want - return false
- else return true
}

希望这会有所帮助。