Sunday, December 15, 2024
HomeiOS Developmentios - Assortment View when no cell is chosen the 0 index...

ios – Assortment View when no cell is chosen the 0 index cell ought to be chosen


I’m a newbie to iOS growth.
I have no idea what’s flawed with my code. I attempted to set the default cell to index 0 when no cell is chosen, however the checkmark doesn’t seem. I might recognize it if somebody might give me a touch to unravel this.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        swap 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.picture {
                    selectedPersonImage.picture = selectedImage
                    profileImage.picture = selectedImage
                    selectedCharacterImage = selectedImage
                    
                    personModels[0].icon = selectedImage
                    selectedCharacterImage = personModels[0].icon
                    selectedImageName = getImageName(for: selectedImage)
                    
//                    Save the choice to UserDefaults
                    userDefaults.set(["section": indexPath.section, "item": indexPath.row], forKey: "selectedPersonIndex")
                    
                    
                }
            }
            else
            {
                // Deal with the case the place cell is nil
                print("Cell is nil for indexPath: (indexPath)")
            }
            
        case 3:
            // Deal with choice for collectionView with tag 3
            
            guard lastSelectedIndexPath != indexPath else { return }
            
            if let selectedCell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell1 {
                selectedCell.isSelected = true
                lastSelectedIndexPath = indexPath
                
                // Replace the pictures and fashions primarily based on the chosen picture
                
                if let selectedImage = selectedCell.plantImg.picture {
                    plantProfileImage.picture = selectedImage
                    selectedPlantImage.picture = selectedImage
                    selectedCharacterImage = selectedImage
                    
                    plantModels[0].icon = selectedImage
                    selectedCharacterImage = plantModels[0].icon
                    
                    let water_Type = userDefaults.string(forKey: "Water_Type")
                    swap water_Type
                    {
                    case "plant":  // return plant knowledge
                        selectedImageName = getImageName(for: selectedImage)
                        userDefaults.set(["section": indexPath.section, "item": indexPath.row], forKey: "selectedPlantIndex")
                    case "animal": // return animal knowledge
                        selectedImageName = getImageName(for: selectedImage)
                        userDefaults.set(["section": indexPath.section, "item": indexPath.row], forKey: "selectedAnimalIndex")
                    default:
                        break
                    }
                }
            } else {
                // Deal with the case the place cell is nil
                print("Cell is nil for indexPath: (indexPath)")
            }
        default:
            break
        }
        settingsTView.reloadData()
    }

the test mark ought to present when no cell is chosen

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments