๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โ—๏ธError

[iOS Error] UICollectionView ReusableView ์—๋Ÿฌ

by Fomagran ๐Ÿ’ป 2021. 11. 10.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ๐Ÿ’ป ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜ ๋งŒ๋‚œ ์—๋Ÿฌ๋Š” ๋ฐ”๋กœ ์œ„์™€ ๊ฐ™์ด 'NSInternalInconsistencyException' ์˜ค๋ฅ˜ ์˜€์Šต๋‹ˆ๋‹ค.

 

 

์ด๊ฒŒ ๋ฌด์Šจ ๋ง์ด๋ƒ๋ฉด ์ปฌ๋ ‰์…˜๋ทฐ์—์„œ ReusableView๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ dequeReusableCell๋กœ ๋ณดํ†ต๊ณผ ๊ฐ™์ด ์„ธํŒ…ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

์ œ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ปฌ๋ ‰์…˜๋ทฐ ํ—ค๋”์™€ ํ‘ธํ„ฐ๋ฅผ ReusableView๋กœ ๋งŒ๋“ค์–ด์„œ ์„ธํŒ…ํ–ˆ๋Š”๋ฐ์š”.

 

์•„๋ž˜์™€ ๊ฐ™์ด dequeํ•˜๋Š” ๋ถ€๋ถ„์ด ์ผ๋ฐ˜ ์…€๊ณผ ๊ฐ™์ด dequeReusableCell๋กœ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

 

  func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader{
            let cell = collection.dequeueReusableCell(withReuseIdentifier: "VoiceChatCreateHeaderCollectionViewCell", for: indexPath) as! VoiceChatCreateHeaderCollectionViewCell
            cell.bind(reactor: reactor)
            return cell
        }else {
            let cell = collection.dequeueReusableCell(withReuseIdentifier: "VoiceChatCreateFooterCollectionViewCell", for: indexPath) as! VoiceChatCreateFooterCollectionViewCell
            cell.bind(reactor: reactor)
            return cell
        }
    }

 

ํ•ด๊ฒฐ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์ด dequeReusableCell๋ฅผ dequeReusableSupplementaryView๋กœ ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader{
            let cell = collection.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "VoiceChatCreateHeaderCollectionViewCell", for: indexPath) as! VoiceChatCreateHeaderCollectionViewCell
            cell.bind(reactor: reactor)
            return cell
        }else {
            let cell = collection.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "VoiceChatCreateFooterCollectionViewCell", for: indexPath) as! VoiceChatCreateFooterCollectionViewCell
            cell.bind(reactor: reactor)
            return cell
        }
    }
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€