Некоторые из ячеек представления коллекции didSelectItemAtIndexPath не работают

введите описание изображения здесь

Я был поражен этим некоторое время. Моя ошибка для некоторых ячеек в расписании didSelectItemAtIndexPath не вызывает. я сохранил точку останова на методе, и когда я щелкаю по некоторым ячейкам, метод не вызывается. В чем может быть проблема. Пожалуйста, смотрите код ниже

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{

       return 25;

}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *) collectionView
{

          return 1;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"item clicked");
    CGRect rect = [collectionData layoutAttributesForItemAtIndexPath:indexPath].frame;

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController* vc = [storyboard instantiateViewControllerWithIdentifier:@"note"];

    pc = [[UIPopoverController alloc] initWithContentViewController:vc];

    [pc presentPopoverFromRect:rect inView:collectionData
      permittedArrowDirections:UIPopoverArrowDirectionAny
                      animated:YES];
    NSLog(@" The index path is %@", indexPath);

}

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


    // we're going to use a custom UICollectionViewCell, which will hold an image and its label
    Cell *myCell = [collectionData dequeueReusableCellWithReuseIdentifier:@"TableCell"
                                                       forIndexPath:indexPath];

    if (indexPath.row < 20 || indexPath.row == 22 )
    {
    myCell.roomLabel.text = [NSString stringWithFormat:@"{%ld,%ld}", (long)indexPath.row, (long)indexPath.section];

    NSLog(@"cell no %ld" ,(long)indexPath.row );
    myCell.subjectLabel.text =@"English";
    myCell.cellView.backgroundColor= [UIColor grayColor];

    [myCell.redXbuttonOutlet addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

    if(isLongPressed)
    {
        [myCell.redXbuttonOutlet setHidden:NO];
    }
    else
    {
       [myCell.redXbuttonOutlet setHidden:YES];
    }
    }
     else
    {

        myCell.userInteractionEnabled = NO;
      [myCell.redXbuttonOutlet setHidden:YES];
       myCell.cellView.backgroundColor= [UIColor whiteColor];
      myCell.subjectLabel.text =@"";
       myCell.roomLabel.text = @"";

    }
    return myCell;

}
-(void)buttonPressed:(id)sender
{
    NSLog(@"Button");
    UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Delete session" message:@"Do you want to delete this session" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}

1 ответ

Решение

Вы отключили взаимодействие с пользователем некоторых клеток чувак. проверьте код ниже, который находится внутри cellForItemAtIndexPath

else
{

  myCell.userInteractionEnabled = NO; // <-----Note this Line

  [myCell.redXbuttonOutlet setHidden:YES];
  myCell.cellView.backgroundColor= [UIColor whiteColor];
  myCell.subjectLabel.text =@"";
  myCell.roomLabel.text = @"";

}
Другие вопросы по тегам