iOS15 UICollectionView自定义布局,插入项目后,viewForSupplementaryElemen


// 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上,它将从缓存中duque。

不知道为什么。

FIXED UICollectionElementKindSectionHeader在iOS 15中崩溃

在我的收藏视图中,我只使用标题。它适用于iOS 14,但在iOS 15中崩溃。

应用程序在AppDelegate上崩溃,我收到此错误

***由于未捕获的异常"NSInternalConferenceException"而终止应用程序,原因:"从-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
}

替换为SupplementaryViewOfKind:和ofKind:from"MyCollectionReusableView;到UICollectionView.elementKindSectionHeader

这对我有用。

最新更新