Error: Exception NSException * "the view returned from -collectionView:viewForSupplementaryElement
Error: Exception NSException * "the view returned from -collectionView:viewForSupplementaryElement
谁能帮我指出我哪里错了?我的应用因以下错误而崩溃:
Thread 1:
Exception: "the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath (UICollectionElementKindSectionHeader,
<NSIndexPath: 0x8180a74d626573ba> {length = 2, path = 0 - 0})
was not retrieved by calling -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: or is nil
(<UICollectionReusableView: 0x7fd9fbecc400; frame = (0 0; 0 0); layer = <CALayer: 0x6000028c7f60>>)"
方法在这里
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath:
IndexPath) -> UICollectionReusableView {
guard kind == UICollectionView.elementKindSectionFooter else {
// footer
return UICollectionReusableView()
}
if indexPath.section == 1 {
// tabs header
let tabControlHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: ProfileTabsCollectionReusableView.identifier,
for: indexPath) as! ProfileTabsCollectionReusableView
return tabControlHeader
}
let profileHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: ProfileInfoHeaderCollectionReusableView.identifier,
for: indexPath) as! ProfileInfoHeaderCollectionReusableView
return profileHeader
}
错误这样读起来更好
the view returned from
-collectionView:viewForSupplementaryElementOfKind:atIndexPath
was
either
not retrieved by calling
-dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
or
is nil
查看以下代码return问题所在。
return UICollectionReusableView()
在 guard else
早期 return 块内放置一个断点。决定是否要从那里 return dequeued
页眉或页脚。
您必须 return 使用 collectionView.dequeueXXX
调用实例化的页眉/页脚,而不是您自己创建的东西。
我通过更改解决了这个错误
guard kind == UICollectionView.elementKindSectionFooter else {
// footer
return UICollectionReusableView()
}
到
guard kind == UICollectionView.elementKindSectionHeader else {
// footer
return UICollectionReusableView()
}
还要确保在 Main.storyboard
中每个 View Controller
都检查了 Inherit Module From Target
谁能帮我指出我哪里错了?我的应用因以下错误而崩溃:
Thread 1:
Exception: "the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath (UICollectionElementKindSectionHeader,
<NSIndexPath: 0x8180a74d626573ba> {length = 2, path = 0 - 0})
was not retrieved by calling -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: or is nil
(<UICollectionReusableView: 0x7fd9fbecc400; frame = (0 0; 0 0); layer = <CALayer: 0x6000028c7f60>>)"
方法在这里
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath:
IndexPath) -> UICollectionReusableView {
guard kind == UICollectionView.elementKindSectionFooter else {
// footer
return UICollectionReusableView()
}
if indexPath.section == 1 {
// tabs header
let tabControlHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: ProfileTabsCollectionReusableView.identifier,
for: indexPath) as! ProfileTabsCollectionReusableView
return tabControlHeader
}
let profileHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: ProfileInfoHeaderCollectionReusableView.identifier,
for: indexPath) as! ProfileInfoHeaderCollectionReusableView
return profileHeader
}
错误这样读起来更好
the view returned from
-collectionView:viewForSupplementaryElementOfKind:atIndexPath
was
either
not retrieved by calling
-dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
or
is nil
查看以下代码return问题所在。
return UICollectionReusableView()
在 guard else
早期 return 块内放置一个断点。决定是否要从那里 return dequeued
页眉或页脚。
您必须 return 使用 collectionView.dequeueXXX
调用实例化的页眉/页脚,而不是您自己创建的东西。
我通过更改解决了这个错误
guard kind == UICollectionView.elementKindSectionFooter else {
// footer
return UICollectionReusableView()
}
到
guard kind == UICollectionView.elementKindSectionHeader else {
// footer
return UICollectionReusableView()
}
还要确保在 Main.storyboard
中每个 View Controller