UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Problem: Project 10 how to apply value change to data source when selecting an item in collectionView.

Forums > 100 Days of Swift

@Haibo  

Here I could see person.name = newName.text! it changes person.name and I could see the cell text was changed in app. But we don't do anything about data source people, so how it is affected here? Should we write people[indexPath.item].name = newName.text! instead?

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let person = people[indexPath.item]

        let ac = UIAlertController(title: "Rename person", message: nil, preferredStyle: .alert)
        ac.addTextField()

        ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))

        ac.addAction(UIAlertAction(title: "OK", style: .default) { [unowned self, ac] _ in
            let newName = ac.textFields![0]
            person.name = newName.text!

            self.collectionView?.reloadData()
        })

        present(ac, animated: true)
    }

3      

@Haibo  

Solved! The reason is that person is assigned with an instanse of class Person, so it is reference type, then what changed in person will changed too in related element in people array.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.