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
'Programming > Swift' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Swift] CocoaPod vs Swift Package Manager (0) | 2023.01.26 |
---|---|
[Swift] tap gesture (0) | 2022.11.21 |
[Swift] CollectionView Cell _ Lingo Feedback (0) | 2022.11.01 |
[Swift] λ€λ₯Έ file (viewController) κ°μ μ‘°μ (0) | 2022.08.31 |
π firestore database μ κ·Ό λ° μμ code, firestore Queries π (0) | 2022.08.28 |