[UI-Kit] CollectionView Cell Selection / Deselection

2022. 11. 15. 17:51ใ†Programming/Swift

 

 

 

 

# shouldSelecItemAt

    func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
        guard let cell = collectionView.cellForItem(at: indexPath) as? SelectPlanetCollectionViewCell else {
            return true
        }
        
        if cell.isSelected {
            // TODO: ํƒ์ƒ‰ ๋ชจ๋“œ๋กœ ๋ณ€ํ™˜
            cell.planetNameLabel.textColor = .white
            cell.backgroundView = nil
            self.navigationController?.topViewController?.title = "๋น ๋ฅด๊ฒŒ ์ฒœ์ฒด ์ฐพ๊ธฐ"
            collectionView.deselectItem(at: indexPath, animated: true)
            return false
        } else {
            return true
        }
    }

 

 

 

# didSelectItemAt

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let cell = collectionView.cellForItem(at: indexPath) as? SelectPlanetCollectionViewCell else { return }
           
        if cell.isSelected {
            cell.planetNameLabel.textColor = .black
            cell.backgroundView = cell.planetBackgroundView
            
            // navigation title ๋ณ€๊ฒฝ
            self.navigationController?.topViewController?.title = "\(cell.planetNameLabel.text ?? "์ฒœ์ฒด") ํƒ์ƒ‰ ์ค‘"
            self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedString.Key.foregroundColor: UIColor.white]
            self.navigationController?.navigationBar.backgroundColor = .black
        }
    }

 

 

 

# didDeselectItemAt

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        if let cell = collectionView.cellForItem(at: indexPath) as? SelectPlanetCollectionViewCell {
            cell.planetNameLabel.textColor = .white
            cell.backgroundView = nil
        }
    }

 

 

 

 

 

 

shouldSelectItemAt

true -> didSelectItemAt์„ ํ˜ธ์ถœ

๋™์ผํ•œ cell์„ ์žฌ์„ ํƒํ–ˆ์„ ๋•Œ, ์ทจ์†Œ ๋™์ž‘ (false return)

true๋ฅผ returnํ•˜๋ฉด ์„ ํƒ์„ ํ—ˆ์šฉ / false๋ฅผ returnํ•˜๋ฉด ์„ ํƒ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ

 

 

didDeselectItemAt

Programmatically ํ˜ธ์ถœ์„ ์•ˆํ•œ๋‹ค.

๋‹ค๋ฅธ cell์„ ์„ ํƒํ–ˆ์„ ๋•Œ, ์ทจ์†Œ ๋™์ž‘

This method doesnโ€™t cause any selection-related delegate methods to be called.

-> ์ด๊ฒŒ ๊ณ„์† ๋‚˜์™”๋˜ programmatically ์„ ํƒ . ์„ ํƒ ์ทจ์†Œ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ?

- programmatically์—์„œ๋Š” shouldSelectItemAt, didSelectItemAt, didDeselectItemAt ์ด๋Ÿฐ ํ•จ์ˆ˜๋“ค ํ˜ธ์ถœ ์•ˆํ•œ๋‹ค๋Š” ๋‚ด์šฉ

 

 

 

 

 

 

# ๊ณต์‹ ๋ฌธ์„œ ๊ณต๋ถ€

1. didSelectItemAt

The collection view calls this method when the user successfully selects an item in the collection view.

It does not call this method when you programmatically set the selection.

https://developer.apple.com/documentation/uikit/uicollectionviewdelegate/1618032-collectionview

 

 

 

 

2. didDeselectItemAt

The collection view calls this method when the user successfully deselects an item in the collection view.

It does not call this method when you programmatically deselect items.

deselect : ๋‹ค๋ฅธ cell์„ ์„ ํƒํ•˜๊ฒŒ ๋˜๋ฉด, ๊ธฐ์กด์— ์„ ํƒ๋œ cell์ด deselect ๋˜๋Š” ๊ฒƒ (์ฆ‰ ์„ ํƒ์ด๋ผ๋Š” ํ–‰์œ„์˜ ๋์„ ์˜๋ฏธํ•จ)

์šฐ๋ฆฌ๋Š” ๋‹ค๋ฅธ cell์„ ํƒํ•  ๋•Œ / ์„ ํƒ๋œ cell์„ ๋‹ค์‹œ ํƒํ•  ๋•Œ (๋‘๊ฐ€์ง€ ๋ชจ๋‘ '์„ ํƒ'์— ์ง‘์ค‘๋œ ๋™์ž‘)

Deselct ์—์„œ ๊ตฌํ˜„ํ•  ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, select์˜ ์ˆœ์„œ์— ์ฃผ์˜ํ•ด์•ผ ํ–ˆ๋‹ค. (bool List -> Should Select item at)

https://developer.apple.com/documentation/uikit/uicollectionviewdelegate/1618035-collectionview

 

 

 

3. shouldSelectItemAt

Asks the delegate if the specified item should be selected. 

The collection view calls this method when the user tries to select an item in the collection view.
It does not call this method when you programmatically set the selection. 
true if the item should be selected or false if it should not. 
์•„์ดํ…œ์ด ์„ ํƒ๋˜์–ด์•ผ ํ•œ๋‹ค๋ฉด -> true ๋ฐ˜ํ™˜ / ์•„์ดํ…œ์ด ์„ ํƒ๋˜์–ด์•ผ ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด -> false ๋ฐ˜ํ™˜

์–ธ์ œ ์•„์ดํ…œ์ด ์„ ํƒ๋˜๋ฉด ์•ˆ๋œ๋‹ค๋Š”๊ฑด๋ฐ? -> ์ด๋ฏธ ์„ ํƒ๋œ ์•„์ดํ…œ์— ๋Œ€ํ•˜์—ฌ

๋„Œ ์ด๋ฏธ ์„ ํƒ ๋™์ž‘์ด ์ด๋ฃจ์–ด์ง„ ์ดํ›„๋‹ˆ๊นŒ, ์žฌ์„ ํƒ ๊ด€๋ จ ์ฝ”๋“œ๋“ค๊ณผ ํ•จ๊ป˜, return false
If you do not implement this method, the default return value is true.

https://developer.apple.com/documentation/uikit/uicollectionviewdelegate/1618095-collectionview

 

 

 

 

4. deselectItem

Deselects the item at the specified index.

If the allowsSelection property is false, calling this method has no effect. 
This method doesnโ€™t cause any selection-related delegate methods to be called.
์ด๊ฒŒ ์˜๋ฏธํ•˜๋Š” ๋ฐ” -> deselectItem == Programmatically deselect items ์˜๋ฏธ!  
https://developer.apple.com/documentation/uikit/uicollectionview/1618040-deselectitem

 

 

 

5. selectItem

Selects the item at the specified index path and optionally scrolls it into view.

func selectItem(
    at indexPath: IndexPath?,
    animated: Bool,
    scrollPosition: UICollectionView.ScrollPosition
)
 
 

scrollPosition : An option that specifies where the item should be positioned when scrolling finishes.
If the allowsSelection property is false, calling this method has no effect.
If thereโ€™s an existing selection with a different index path and the
 allowsMultipleSelection property is false, calling this method replaces the previous selection.
This method doesnโ€™t cause any selection-related delegate methods to be called.
https://developer.apple.com/documentation/uikit/uicollectionview/1618057-selectitem