iOS15 UICollectionView таможенный макет, после вставки элемента viewForSupplementaryElementOfKind всегда воссоздает новый заголовок

      // viewdidload register
[self.collectionView registerClass:[TestHeaderCollectionReusableView class]
            forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                   withReuseIdentifier:NSStringFromClass(NEHeaderCollectionReusableView.class)];

// viewForSupplementaryElementOfKind called
TestHeaderCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                                  withReuseIdentifier:NSStringFromClass(TestHeaderCollectionReusableView.class)
                                                                                         forIndexPath:indexPath];

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

на iOS15 после вставки элемента

      - (void)insertItem {
    [self.dataSourece.firstObject insertObject:@{
        @"title" : @"ins1"
    } atIndex:0];
    NSIndexPath * path= [NSIndexPath indexPathForItem:0 inSection:0];
    [self.collectionView insertItemsAtIndexPaths:@[path]];
}

он всегда будет воссоздавать TestHeaderCollectionReusableView в первый раз после того, как я вставлю элемент. но на iOS14 он будет извлекаться из кеша.

не знаю почему.

1 ответ

ИСПРАВЛЕНО сбой UICollectionElementKindSectionHeader в iOS 15

В моем collectionView я использую только заголовки. Он работает для iOS 14, но в iOS 15 вылетает.

Сбой приложения на AppDelegate, и я получаю эту ошибку

*** Завершение работы приложения из-за неперехваченного исключения «NSInternalInconsistencyException», причина: «представление, возвращенное из -collectionView:viewForSupplementaryElementOfKind:atIndexPath: не соответствует типу элемента, для которого оно используется. При запросе представления типа элемента «UICollectionElementKindSectionHeader» источник данных исключил из очереди представление, зарегистрированное для типа элемента «MyCollectionReusableView».

Заголовок регистра

          collectionView.register(UINib(nibName: "MyCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "MyCollectionReusableView")

Затем в методе UICollectionViewDataSource

          func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "MyCollectionReusableView", for: indexPath) as! MyCollectionReusableView
        headerView.backgroundColor = .red
        // Configure header view .....
        return headerView
    }

Замените forSupplementaryViewOfKind: и ofKind: с MyCollectionReusableView на UICollectionView.elementKindSectionHeader.

Это работает для меня.

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