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

Day 44, Project 10, challenge 1: How to delete a person's image?

Forums > 100 Days of Swift

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

  1. Delete the edited image off of the collectionView
  2. 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)
}

3      

So here's what I gathered, with the help from Prathamesh Kowarkar on the course's Slack channel

  1. Delete the edited image off of the collectionView
  2. Delete the edited image off of the disk, too

The 1st one is done by:

  • Removing the item at the current index path from the people array
  • Either:
    • Reloading the collectionView, or
    • Deleting the item from the collectionView, too (I prefer this approach more, since it's more semantically correct, and you could also notice the subtle fade effect using this one)

Here's the particular code for the Delete action

ac.addAction(UIAlertAction(title: "Delete", style: .destructive) { [weak self] _ in
                self?.people.remove(at: indexPath.item)
                self?.collectionView.reloadData() // either this one
                self?.collectionView.deleteItems(at: [indexPath]) // or this one
            })

I'm still not sure about how to delete the edited image from the disk. If I recall correctly, Paul said it's gonna be mentioned in Project 12, so I'm looking forward to learning about that.

3      

Why you want to delete the image? We need to show the second alert to replace the current person name from the image or to delete it. This is what I'm trying to work out at the moment as well, self?.people.remove(at: indexPath.item) will automatically delete the image from the cell I tried this one as well. We want to show the second alert when we tap second time on the picture, because first time when we tap on it first alert will come up to add a person name, second time we want to rename or delete the person name. This would make more sense a bit but yeah we can delete the image as well but that would be a different condition there.

3      

Challange says that we need to use a second alert, we need to track the tapped status somehow, from what I see you are using an actionsheet. Because first time when we tap the imagine the first alert will pop, and when we tap second time a different alert should be displayed, this has more logic.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.