iOS

[ iOS ] cell ๋‚ด ๋ฒ„ํŠผ ์„ ํƒ ์ด๋ฒคํŠธ ๊ตฌํ˜„ (+ Delegate ํŒจํ„ด)

Forest Yun 2022. 6. 14. 23:27
728x90

๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

 

 

๋ฌธ์ œ

์‚ฌ์ง„ ๋‚ด ๋ฒ„ํŠผ ์„ ํƒํ•˜๊ธฐ

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

1.  cell ๋‚ด ๋ฒ„ํŠผ ์ด๋ฒคํŠธ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ

UICollectionViewControllerDelegate์˜ collectionView(_:didSelectItemAt:) ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด collectionView Cell์˜ ์„ ํƒ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ์ด๋ฒคํŠธ๋Š” cell์˜ ์ด๋ฒคํŠธ๋ฅผ ์˜๋ฏธํ•˜์ง€ cell ๋‚ด ๋ฒ„ํŠผ์˜ ์ด๋ฒคํŠธ๋ฅผ ์˜๋ฏธํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. ์šฐ๋ฆฌ๋Š” ๋ฒ„ํŠผ์ด cell ๋‚ด์— ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.

collectionView(_:didSelectItemAt:)
๋ธ๋ฆฌ๊ฒŒ์ดํŠธ์— ํŠน์ •ํ•œ ์ธ๋ฑ์Šค์˜ ์•„์ดํ…œ์ด ์„ ํƒ๋˜์—ˆ์Œ์„ ์•Œ๋ฆฐ๋‹ค.

 

๋ฒ„ํŠผ์— ์„ ํƒ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํ•ด๋‹นํ•˜๋Š” ์•ก์…˜ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฏ€๋กœ, ์•ก์…˜ํ•จ์ˆ˜ ๋‚ด์— ์‚ฌ์ง„์„ ์„ ํƒํ•˜๋Š” ์ž‘์—…์„ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค. ํ•˜์ง€๋งŒ ์ „์ฒด ์‚ฌ์ง„๋ชฉ๋ก๊ณผ ์„ ํƒ๋œ ์‚ฌ์ง„ ๋ชฉ๋ก์€ ImagePickerViewController์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ImageCollectionViewCell์—์„œ๋Š” ์›ํ•˜๋Š” ์ž‘์—…ํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

 

 

 

 

 

 

 

 

 

2. Delegate ํŒจํ„ด

 

 

 

[ IOS ] ํ”ผ์ปค ๋ทฐ ๋งŒ๋“ค๊ธฐ ( ํ”„๋กœํ† ์ฝœ, ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ )

๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ๋ฌธ์ œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‘ ๊ฐœ์ธ ํ”ผ์ปค ๋ทฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. ์™ผ์ชฝ ํ”ผ์ปค ๋ทฐ๋ฅผ ์„ ํƒํ–ˆ์„ ๊ฒฝ์šฐ ํŒŒ์ผ ๋ช…์ด ๋‚˜ํƒ€๋‚˜๋„๋ก, ์˜ค๋ฅธ์ชฝ ํ”ผ์ปค ๋ทฐ๋ฅผ ์„ ํƒํ–ˆ์„ ๊ฒฝ์šฐ ์ด๋ฏธ์ง€ ๋ทฐ์— ์ด๋ฏธ์ง€๋ฅผ ์ถœ

88yhtserof.tistory.com

Delegate
delegate๋Š” '์œ„์ž„ํ•˜๋‹ค.'๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋ฉฐ ํ”„๋กœํ† ์ฝœ๋กœ ๊ตฌํ˜„๋œ๋‹ค.
์˜ˆ) A.delegate = B
A๊ฐ€ ํ•ด์•ผํ•  ์ž‘์—…์„, B๊ฐ€ ๊ถŒํ•œ์„ ์œ„์ž„๋ฐ›์•„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

 

Delegate ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด ImageCollectionViewCell์—์„œ ์ด๋ฃจ์–ด์ ธ์•ผ ํ•  ์ž‘์—…์„ ImagePickerViewController์—์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ImagePickerViewController์—์„œ ๊ตฌํ˜„ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ๋ฒ„ํŠผ์˜ ์•ก์…˜ํ•จ์ˆ˜์—์„œ ํ˜ธ์ถœํ•˜๋ฉด ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

//ImageCollectionViewCell.swift

protocol ImageCollectionViewCellDelegate: AnyObject {
    func didSelectCountButton(_ cell: ImageCollectionViewCell)
}

class ImageCollectionViewCell: UICollectionViewCell {
    ...
    weak var delegate: ImageCollectionViewCellDelegate?
    ...
    
    private func ๋ฒ„ํŠผ์˜ ์•ก์…˜ํ•จ์ˆ˜(){
        delegate?.didSelectCountButton(self)
    }
}
  • delegate๋Š” ์ฐธ์กฐ์ˆœํ™˜์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด weak ํƒ€์ž…์œผ๋กœ ์„ ์–ธ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ, weak ํƒ€์ž…์€ ์—ด๊ฑฐํ˜•, ๊ตฌ์กฐ์ฒด ๋“ฑ ํด๋ž˜์Šค๊ฐ€ ์•„๋‹Œ ๊ณณ์—์„œ๋Š” ์‚ฌ์šฉ๋  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ํ•ด๋‹น ํ”„๋กœํ† ์ฝœ์ด ํด๋ž˜์Šค์—์„œ๋งŒ ์ฑ„ํƒ๋œ๋‹ค๋Š” ๊ฒƒ์„ ๋ช…์‹œํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด AnyObject๋ฅผ ์ƒ์†ํ•ด์ค€๋‹ค.
  • AnyObject: ํ•ด๋‹น ํƒ€์ž…์„ ์ƒ์†๋ฐ›๋Š” ํ”„๋กœํ† ์ฝœ์€ ์•”๋ฌต์ ์œผ๋กœ ํด๋ž˜์Šค์—๋งŒ ์ฑ„ํƒ๋˜๋Š” ํ”„๋กœํ† ์ฝœ์ด๋‹ค.

 

 

//ImagePickerViewController.swift

extension ImagePickerViewController: ImageCollectionViewCellDelegate {//ํ”„๋กœํ† ์ฝœ ์ฑ„ํƒ
    func didSelectCountButton(_ cell: ImageCollectionViewCell) {
        ...
    }
}

 

 

 

 

 

 

 

 

 

 

3. delgate ์œ„์ž„๋ฐ›๊ธฐ

์ •๋ฆฌ๋ฅผ ํ•˜์ž๋ฉด, ImageCollectionViewCell(cell)์˜ ๋ฉ”์„œ๋“œ๋ฅผ ImagePickerViewController(collection)์—์„œ ๋Œ€์‹  ๊ตฌํ˜„ํ•ด์ฃผ๋Š” ๊ณผ์ •์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด ๋‘˜์€ ์–ด๋–ป๊ฒŒ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์„๊นŒ? ๋ฐฉ๋ฒ•์€ delegate ํ”„๋กœํผํ‹ฐ์ด๋‹ค. ์œ„์ž„๋ฐ›๋Š” ํด๋ž˜์Šค์—์„œ ํ•ด๋‹น delegate ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๊ณ  ์œ„์ž„ํ•˜๋Š” ํด๋ž˜์Šค์˜ delegate ํ”„๋กœํผํ‹ฐ์— ์ž์‹ (self)์„ ํ• ๋‹นํ•˜๋ฉด ์„œ๋กœ ์—ฐ๊ฒฐ๋œ๋‹ค.

ImageCollectionViewCell.delegate = self

 

 

 

 

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ๊ฐ cell ๋งˆ๋‹ค ์ž‘์—…์„ ์œ„์ž„ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ์ ์ด๋‹ค. ๋ชจ๋“  ์‚ฌ์ง„์€ ์„ ํƒ ๋ฒ„ํŠผ์ด ์กด์žฌํ•œ๋‹ค. ์ฆ‰, ๋ชจ๋“  cell์—์„œ ์„ ํƒ ์ด๋ฒคํŠธ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ cell ๋งˆ๋‹ค ์ž‘์—…์„ ImageCollectionViewController์— ์œ„์ž„ํ•ด์ฃผ๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค.

ImageCollectionViewController ํด๋ž˜์Šค์—์„œ ๋ชจ๋“  cell๋“ค์„ ๊ฐ๊ฐ ๋งŒ๋‚˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๋Š”๋ฐ, ๋ฐ”๋กœ UICollectionViewControllerDataSource์˜ collectionView(_:cellForItemAt:) ๋ฉ”์„œ๋“œ์ด๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” cell์„ ๊ตฌ์„ฑํ•˜์—ฌ collctionView์— ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋ฏ€๋กœ ๋ชจ๋“  cell์˜ ํด๋ž˜์Šค์— ์ ‘๊ทผํ•œ๋‹ค.

//ImagePickerViewController.swift

extension ImagePickerViewController: UICollectionViewDataSource {
    ...
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = self.imageCollectionView.dequeueReusableCell(withReuseIdentifier: ImageCollectionViewCell.identifier, for: indexPath) as? ImageCollectionViewCell else {return UICollectionViewCell()}
        
        cell.delegate = self
        ...
        
        return cell
    }
}

 

 

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

Delegate
Delegate ํŒจํ„ด์ด๋ž€?
Delegate ํŒจํ„ด์„ ์‚ฌ์šฉํ•  ๋•Œ AnyObject๋ฅผ ์ƒ์†๋ฐ›๋Š” ์ด์œ ์™€ weak๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 
728x90