[ios_Swift] 콜렉션뷰 filter 기능 버튼 만들기 (1)

이시영·2023년 11월 7일

콜렉션뷰로 필터기능을 가진 버튼을 만들기 위해 먼저 버튼과 이를 선택할 수 있는 기능을 먼저 만들었다.

셀을 탭했다는 정보를 저장하고 활용하기 위해 변수들을 선언해주었다.

var selectedTierIndexPath: IndexPath?
var selectedPositionIndexPath: IndexPath?
var selectedTierOption: String?
var selectedPositionOption: String?

그리고 유저디폴트를 사용하여 이를 저장하고 불러오는 매써드도 만들어주었다.

func saveSelectedCellInfo() {
        let defaults = UserDefaults.standard
        defaults.set(selectedTierIndexPath?.row, forKey: "selectedTierIndex")
        defaults.set(selectedPositionIndexPath?.row, forKey: "selectedPositionIndex")
    }
    
    func loadSelectedCellInfo() {
        let defaults = UserDefaults.standard
        if let tierIndex = defaults.value(forKey: "selectedTierIndex") as? Int,
           let positionIndex = defaults.value(forKey: "selectedPositionIndex") as? Int
        {
            selectedTierIndexPath = IndexPath(row: tierIndex, section: 0)
            selectedPositionIndexPath = IndexPath(row: positionIndex, section: 1)
        }
    }

그리고 이를 didSelectItemAt 에 적용하여 기능을 구현하였다.

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            if indexPath == selectedTierIndexPath {
                // 이미 선택된 셀을 다시 탭한 경우, 선택 해제
                selectedTierIndexPath = nil
                selectedTierOption = "default"
                UserDefaults.standard.removeObject(forKey: "selectedTierIndexPath")
                
                if let selectedCell = collectionView.cellForItem(at: indexPath) as? TierCell {
                    selectedCell.tierLabel.layer.borderColor = UIColor.systemGray4.cgColor

                }
            } else {
                if let previousIndexPath = selectedTierIndexPath {
                    if let previousCell = collectionView.cellForItem(at: previousIndexPath) as? TierCell {
                        previousCell.tierLabel.layer.borderColor = UIColor.systemGray4.cgColor
                    }
                }

                selectedTierIndexPath = indexPath

                if let selectedCell = collectionView.cellForItem(at: indexPath) as? TierCell {
                    selectedCell.tierLabel.layer.borderColor = UIColor.systemBlue.cgColor
                    selectedTierOption = tierName[indexPath.row]
                }
            }
        } else {
            if indexPath == selectedPositionIndexPath {
                // 이미 선택된 셀을 다시 탭한 경우, 선택 해제
                selectedPositionIndexPath = nil
                selectedPositionOption = "default"
                UserDefaults.standard.removeObject(forKey: "selectedPositionIndexPath")

                if let selectedCell = collectionView.cellForItem(at: indexPath) as? PositionCell {
                    selectedCell.positionFrame.layer.borderColor = UIColor.systemGray4.cgColor
                }
            } else {
                if let previousIndexPath = selectedPositionIndexPath {
                    if let previousCell = collectionView.cellForItem(at: previousIndexPath) as? PositionCell {
                        previousCell.positionFrame.layer.borderColor = UIColor.systemGray4.cgColor
                    }
                }

                selectedPositionIndexPath = indexPath

                if let selectedCell = collectionView.cellForItem(at: indexPath) as? PositionCell {
                    selectedCell.positionFrame.layer.borderColor = UIColor.systemBlue.cgColor
                    selectedPositionOption = positionName[indexPath.row]
                }
            }
        }
    }

위 코드로
1) 선택된 셀의 보더컬러가 바뀌도록 만들었고
2) 선택된 셀들을 변수들에 저장하여 활용할 수 있독록 만들었다.(다른 페이지로 정보 전달)
3) 유저디폴트에도 정보를 저장함으로 다시 필터기능 선택창을 열었을때 기존의 선택 정보가 반영되도록 만들었으며
4) 선택된 셀을 다시 누른다면 선택을 취소시키고, 선택정보를 초기화시키는 기능을 구현하였다.

0개의 댓글