G’day guys. I’m currently at day 44 of #100DaysWithSwift
.
Project 10, challenge 1 asks for an alert controller to allow the users either renaming or deleting the person in UICollectionView
. So far, I was able to get it up and running, but not the part with the Delete function.
👉🏻 I’m not sure if Paul would show it in future projects of the course, but I’d appreciate it if anyone could help me with it?
So far I only know that the Delete action is supposed to do 2 things
- Delete the edited image off of the
collectionView
- Delete the edited image off of the disk, too
I’ve attached my didSelectItemAt()
method here, just in case. Thank you in advance 🙏🏻
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// Allow changing a person's name, or delete, upon tapping a specific collection view item
let person = people[indexPath.item]
let acMain = UIAlertController(title: "Options", message: "Choose to ename or delete the person", preferredStyle: .actionSheet)
// Define the Rename alert
let renameAction = UIAlertAction(title: "Rename", style: .default) { [weak self] (action) in
let ac = UIAlertController(title: "Rename person", message: nil, preferredStyle: .alert)
ac.addTextField()
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
ac.addAction(UIAlertAction(title: "OK", style: .default) { [weak self, weak ac] _ in
guard let newName = ac?.textFields?[0].text else { return }
person.name = newName
self?.collectionView.reloadData()
})
self?.present(ac, animated: true)
}
// Define the Delete alert
let deleteAction = UIAlertAction(title: "Delete", style: .destructive) { [weak self] (action) in
let ac = UIAlertController(title: "Confirm delete", message: "Are you sure you would like to delete?", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
ac.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: nil)) // temp nil for now
self?.present(ac, animated: true)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
// Add all the actions to acMain
acMain.addAction(renameAction)
acMain.addAction(deleteAction)
acMain.addAction(cancelAction)
present(acMain, animated: true)
}