2022. 11. 1. 17:36ใProgramming/Swift
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SelectPlanetCollectionViewCell.identifier, for: indexPath) as? SelectPlanetCollectionViewCell else { return UICollectionViewCell() }
let selectedPlanetName = planetList[indexPath.row].planetName
let selectedPlanetImage = planetList[indexPath.row].planetImage
cell.planetNameLabel.text = selectedPlanetName
cell.planetImageView.image = selectedPlanetImage
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SelectPlanetCollectionViewCell.identifier, for: indexPath) as? SelectPlanetCollectionViewCell
let selectedPlanet = planetList[indexPath.row]
if (cell?.isSelected) != nil {
contentsViewController.planet = selectedPlanet
navigationController?.pushViewController(contentsViewController, animated: true)
}
}
1. Cell์ identifier
: ์ฌ๋ฌ ๊ฐ์ Cell ํ์ผ์ด ์์ ์ ์๋ค. (๊ผญ ํ๋์ Cell๋ง ์ฌ์ฉํ๋ ๊ฒ ์๋๋๊น)
-> identifier๋ฅผ ํตํด ๋ถ๋ฌ์ฌ ์ ์๋๋ก, ํจ์ ๋ด์์ cell ์ ์ธ ์ ํ์ฉํ๋ ๊ฒ
-> cell์ ์ฌ์ฌ์ฉ : dequeueReusableCell
: ์ฌ์ฌ์ฉํ ์ผ์ด ์๋ค๋ฉด, ๊ตณ์ด dequeueReusableCell ์์จ๋ ๋๋ค! ex) ๊ฐ๋จํ ์ค์ ๋ทฐ ๋ฑ๋ฑ
2. " ์๋ฐ์ดํ " ๋ด๋ถ์ ๋ค์ด๊ฐ๋ ๋ฌธ์์ด ์ฃผ์
: ์คํ๊ฐ ๋ฐ์ํด๋, ์ปดํ์ผ๋ฌ๊ฐ ์ธ์ํ ์ ์๋ค. (์ธ๊ฐ ์ฝ์ผ๋ผ๊ณ ๋ง๋ ๊ฒ)
-> ๋ฐ๋ผ์ ํด๋น ๋ด์ฉ๋ค์ ์ฌ๋ฌ๋ฒ ์ฌ์ฉ๋ ๊ฒฝ์ฐ, ๋ฐ๋ก ๋ณ์๋ก ์ ์ธํ๊ณ ์ฐ๋ ๊ฒ์ด ์ข๋ค.
-> ์คํ๊ฐ ๋๋๋ผ๋ OK -> ํด๋น ๋ณ์๋ก ์ฎ์ด๋ฉด ์ฝ๋๋ ์ค๋ฅ ์์ด ๋์๊ฐ๋๊น.
3. Planet์ ๋๊ธฐ๋ฉด ์ข์ง ์์๊น?
-> ๊ธฐ์กด์๋ setPlanet ํจ์๋ฅผ ํตํด, planet ์ด๋ฆ๊ณผ ์ด๋ฏธ์ง ๊ฐ์ ๋๊น
-> ์ดํ ํ์ฑ ๋ชจ๋ธ์ ๋ค๋ฅธ ๋ฐ์ดํฐ๊ฐ ์ถ๊ฐ๋ ์ ์์ผ๋๊น, Planet ์์ฒด๋ฅผ ๋๊ธฐ๋ ๊ฒ์ด ์ข์ง ์์๊น?
-> REFACTOR Commit์ผ๋ก ์์ ํด์ ๋จธ์งํ๋ค :)
'Programming > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] tap gesture (0) | 2022.11.21 |
---|---|
[UI-Kit] CollectionView Cell Selection / Deselection (0) | 2022.11.15 |
[Swift] ๋ค๋ฅธ file (viewController) ๊ฐ์ ์กฐ์ (0) | 2022.08.31 |
๐ firestore database ์ ๊ทผ ๋ฐ ์ญ์ code, firestore Queries ๐ (0) | 2022.08.28 |
โฟ iOS Swift Closure & @escaping ํด๋ก์ ์ ๋ฆฌ (0) | 2022.08.26 |