0

В проекте нет ни одного UITapGestureRecognizer. Множественное выделение разрешено collectionView.allowsMultipleSelection = YES Почему метод didDeselectItemAtIndexPath вызывается только при долгом нажатии ячейки? Это так и задумано? При обычном нажатии вызывается didSelectItemAtIndexPathдаже если ячейка уже выделена.

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    //работа с данными, вызов перезагрузки ячейки для обновления внешнего вида
    [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.collectionView deselectItemAtIndexPath:indexPath animated:YES];
    //работа с данными, вызов перезагрузки ячейки для обновления внешнего вида
    [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
}

1 ответ 1

1

Попробуй сбросить выделение ячейки (даже если его не видно) вот через такой хак:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.selected = YES;
    [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    return cell;
}
1
  • добавил в cellForItemAtIndexPath код if ([self isDateSelected:date]) { cell.selected = YES; [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone]; } else { cell.selected = NO; [self.collectionView deselectItemAtIndexPath:indexPath animated:YES]; } - работает, но странно, почему приходится использовать этот хак 5 июл 2016 в 9:30

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.