[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