[ad_1]
I need to put the header of the gathering view, however I get an error each time I put the header title label worth.
There isn’t a header view, so there’s an erre saying that the non-compulsory worth needs to be unwrapped, however I can not determine it out.
Please assist me.
I need to see the header view.
ERROR : Thread 1: Deadly error: Unexpectedly discovered nil whereas implicitly unwrapping an Elective worth
Here is My Code
class TwenyOneViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
tremendous.viewDidLoad()
collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
collectionView.dataSource = self
collectionView.delegate = self
self.collectionView.collectionViewLayout = createLayout()
//헤더 설정
collectionView.register(HeaderCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HeaderCollectionReusableView")
}
}
//MARK: - CollectionView Format 설정
extension TwenyOneViewController {
// 콤포지셔널 레이아웃 설정
fileprivate func createLayout() -> UICollectionViewLayout {
// 레이아웃 생성 - 섹션 > 아이템 > 그룹 이렇게 포함됨
let structure = UICollectionViewCompositionalLayout{
(sectionIndex: Int, layoutEnvironMent: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
//아이템 사이즈
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.9), heightDimension: .fractionalHeight(1.0))
let merchandise = NSCollectionLayoutItem(layoutSize: itemSize)
//아이템 간 간격 설정
merchandise.contentInsets = NSDirectionalEdgeInsets(prime: 10, main: 5, backside: 5, trailing: 5)
//그룹 사이즈
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(160))
// 그룹 만들기
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: merchandise, depend: 3)
// 그룹으로 섹션 만들기
let part = NSCollectionLayoutSection(group: group)
//섹션에 대한 간격 설정
part.contentInsets = NSDirectionalEdgeInsets(prime: 10, main: 20, backside: 20, trailing: 20)
part.orthogonalScrollingBehavior = .steady
//섹션 헤더
let titleSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(30))
let titleSupplementary = NSCollectionLayoutBoundarySupplementaryItem(
layoutSize: titleSize,
elementKind: UICollectionView.elementKindSectionHeader,
alignment: .prime)
print("=-----(titleSupplementary)")
part.boundarySupplementaryItems = [titleSupplementary]
return part
}
return structure
}
}
//MARK: - UICollectionViewDataSource
extension TwenyOneViewController: UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WorkCollectionViewCell", for: indexPath) as? WorkCollectionViewCell else { return UICollectionViewCell() }
cell.imageView.picture = UIImage(systemName: "rectangle.and.pencil.and.ellipsis")
cell.titleLabel.textual content = "타이틀 (indexPath.row)"
cell.descriptionLabel.textual content = "서브 (indexPath.row)"
//셀 그림자 설정
cell.layer.borderWidth = CGFloat(1)
cell.layer.shadowColor = UIColor.grey.cgColor
cell.layer.shadowOffset = CGSize(width: 4, peak: 5)
cell.layer.shadowRadius = 2
cell.layer.shadowOpacity = 0.2
cell.layer.masksToBounds = false
return cell
}
//섹션의 헤더 사용하기 위해 구현
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind type: String, at indexPath: IndexPath) -> UICollectionReusableView {
change type {
// 1
case UICollectionView.elementKindSectionHeader:
// 2
// Configure the header view right here if wanted
let headerView = collectionView.dequeueReusableSupplementaryView(
ofKind: type,
withReuseIdentifier: "HeaderCollectionReusableView",
for: indexPath)
// 3
guard let typedHeaderView = headerView as? HeaderCollectionReusableView
else { return headerView }
// 4
typedHeaderView.titleLabel.textual content = "수상별 목록"
return typedHeaderView
default:
// 5
assert(false, "Invalid factor kind")
}
}
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection part: Int) -> CGSize{
// Exhibits solely the header of the third part
let dimension = CGSize(width: 20, peak: 20)
return part == 2 ? dimension : .zero // Change desiredSize with the dimensions of the seen header
}
}
[ad_2]

