简体   繁体   中英

Deleting tableview image from Firebase Storage

I've a tableview with a images that users will upload. Now I'm trying to add a delete button if the user does NOT want that image anymore.

Maybe I'm just tired in my head and cannot find a solution for this, but I feel like I'm kinda stuck right now.

So when I click on a tableview row. (DidSelectRow) it should retrieve the Firebase Storage image name. Like the image below:

here you go

I do not understand how I can get the filename from the tableview. Do you know?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    deletePathFromArray = indexPath.row
    deleteName = UUID().uuidString
    print(indexPath)
}

This is what I've so far. (deletePathFromArray will just delete it from the tableview, and not from storage)

Also I've this:

let deleteRef = storageRef.child(deleteName)
        deleteRef.delete { (err) in
            if let err = err
            {
                print(err.localizedDescription)
            }
            else
            {
                print("Successfully deleted image!")
            }
        }

The approach is the issue... This is a conceptual answer and will provide direction about the overall structure and process.

When an image is uploaded to Storage , you will be provided a url which is a refence to that image. That reference should be stored in Firestore or RTDB.

Your tableView should be backed by a dataSource . When the images are initially loaded, a typical design pattern is to have a class or struct that represents that data. For example in the RTDB we would have

users_images
    uid_0
       image_id_0 //created with .childByAutoId
          image_url: "http.... reference to the image in Storage"
          title: "My Visit To Paris"

and a class that would hold that

class UserImageClass {
   var key = ""
   var uid = ""
   var title = ""
   var url = ""
}

then those classes are stored in an array, which is the dataSource for your tableView

class ViewController: NSViewController {
    var imagesArray = [UserImageClass]()

From there if a user swipes to delete, you will know the index of the row they swiped, then you can get the object from the dataSource array. From that, you will know the url reference to the image in Storage, so it can be deleted and then also the key to the node in the RTDB so that can be deleted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM