"Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath: was not retrieved by calling -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: for element kind 'UICollectionElementKindSectionHeader' at index path <NSIndexPath: 0xd3fc3c4392da83d4> {length = 2, path = 0 - 0}; supplementary view: <UICollectionReusableView: 0x7fa9ef9181c0; frame = (0 0; 0 0); layer = <CALayer: 0x6000031f6e60>>' "\n\n这个错误是由于在UICollectionView的代理方法collectionView:viewForSupplementaryElementOfKind:atIndexPath:中返回的补充视图没有通过dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:方法获取而导致的。\n\n解决方法是,在collectionView:viewForSupplementaryElementOfKind:atIndexPath:方法中,确保使用dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:方法获取补充视图,如下所示:\n\nswift\nfunc collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {\n // 使用dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:方法获取补充视图\n let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: \"HeaderView\", for: indexPath) as! HeaderView\n \n // 配置补充视图的内容\n \n return headerView\n}\n\n\n确保在使用dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:方法时,传入的kind参数与方法的kind参数一致,并且withReuseIdentifier参数与在Storyboard或代码中注册补充视图时使用的标识符一致。\n\n这样就可以避免这个异常错误的出现了。

解决iOS UICollectionView 中 'NSInternalInconsistencyException' 错误:补充视图未正确获取

原文地址: https://www.cveoy.top/t/topic/o9Pl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录