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 |