Collection View when no cell is selected the 0 index cell should be selected

I am a beginner to iOS development.
I do not know what is wrong with my code. I tried to set the default cell to index 0 when no cell is selected, but the checkmark does not appear. I would appreciate it if someone could give me a hint to solve this.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch collectionView.tag
        {
        case 1:
            guard lastSelectedIndexPath != indexPath else { return }
            if let index = lastSelectedIndexPath,
               let lastSelectedCell = collectionView.cellForItem(at: index) as? CollectionViewCell1
            {
                lastSelectedCell.isSelected = false
            }
            
            
        case 2:
            guard lastSelectedIndexPath != indexPath else { return }
            if let selectedCell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell1
            {
                selectedCell.isSelected = true
                lastSelectedIndexPath = indexPath
                
                if let selectedImage = selectedCell.imagesView.image {
                    selectedPersonImage.image = selectedImage
                    profileImage.image = selectedImage
                    selectedCharacterImage = selectedImage
                    
                    personModels[0].icon = selectedImage
                    selectedCharacterImage = personModels[0].icon
                    selectedImageName = getImageName(for: selectedImage)
                    
//                    Save the selection to UserDefaults
                    userDefaults.set(["section": indexPath.section, "item": indexPath.row], forKey: "selectedPersonIndex")
                    
                    
                }
            }
            else
            {
                // Handle the case where cell is nil
                print("Cell is nil for indexPath: (indexPath)")
            }
            
        case 3:
            // Handle selection for collectionView with tag 3
            
            guard lastSelectedIndexPath != indexPath else { return }
            
            if let selectedCell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell1 {
                selectedCell.isSelected = true
                lastSelectedIndexPath = indexPath
                
                // Update the images and models based on the selected image
                
                if let selectedImage = selectedCell.plantImg.image {
                    plantProfileImage.image = selectedImage
                    selectedPlantImage.image = selectedImage
                    selectedCharacterImage = selectedImage
                    
                    plantModels[0].icon = selectedImage
                    selectedCharacterImage = plantModels[0].icon
                    
                    let water_Type = userDefaults.string(forKey: "Water_Type")
                    switch water_Type
                    {
                    case "plant":  // return plant data
                        selectedImageName = getImageName(for: selectedImage)
                        userDefaults.set(["section": indexPath.section, "item": indexPath.row], forKey: "selectedPlantIndex")
                    case "animal": // return animal data
                        selectedImageName = getImageName(for: selectedImage)
                        userDefaults.set(["section": indexPath.section, "item": indexPath.row], forKey: "selectedAnimalIndex")
                    default:
                        break
                    }
                }
            } else {
                // Handle the case where cell is nil
                print("Cell is nil for indexPath: (indexPath)")
            }
        default:
            break
        }
        settingsTView.reloadData()
    }

the check mark should show when no cell is selected

New contributor

iOS Developer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật