Home > Blockchain >  Accessing a String from dynamic tableView by tapping
Accessing a String from dynamic tableView by tapping

Time:06-05

I have a tableView which get some data from Firebase. I want to get for example the String name when a user taps on a row.

This is how I generate my tableView.

    ref = Database.database().reference().child("placeID/\(placeIdFromSearch)")
    ref.queryOrdered(byChild: "userTime").queryLimited(toLast: 10).observe(DataEventType.value, with: {(snapshot) in
        
        if snapshot.childrenCount > 0 {
            self.table.removeAll()
            
            for video in snapshot.children.allObjects as! [DataSnapshot] {
                let Object = video.value as? [String: AnyObject]
                
                let userName = Object?["userName"]
                let userGroup = Object?["userGroup"]
                let userComment = Object?["userComment"]
                let userTime = Object?["userTime"]
                let userLikes = Object?["userLikes"]
                
                let video = Videos(userName: userName as! String, userGroup: userGroup as! String, userComment: userComment as! String, userTime: userTime as! Int, userLikes: userLikes as! String)
                // die zeile hier zeigt es anders herum.
                self.table.insert(video, at: 0)
               // self.table.append(video)
                self.tableView.reloadData()
             
                //print(Object)
            }
        }
        
    })

With this I can see if someone tapped a row and even can see which row has been tapped.

extension FirstTabSecondViewRight: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("you tapped me!")
        print(indexPath.row)
        self.performSegue(withIdentifier: "CommentDetail", sender: Any?.self)
        
        
        
    }
}

But how can I get for example the String userName of the tapped row?

I tried using print(Object) and then it shows me few dictionaries but I cannot access them by saying Object[0]

The result of print(Object) is this:

["userComment": Achter Beitrag , "userTime": 1654364777411, "userLikes": 8 Likes, "userID": wOEdaJ3BW9Wlu8lKtJDmTlLP40O2, "userGroup": Physiomed Physiotherapeut, "userName": Peter]
["userGroup": Physiomed Physiotherapeut, "userLikes": 8 Likes, "userTime": 1654364865400, "userName": Peter, "userID": wOEdaJ3BW9Wlu8lKtJDmTlLP40O2, "userComment": Neunter Beitrag]
["userGroup": Physiomed Physiotherapeut, "userLikes": 8 Likes, "userTime": 1654365021690, "userName": Peter, "userID": wOEdaJ3BW9Wlu8lKtJDmTlLP40O2, "userComment": Zehnter Beitrag ]

CodePudding user response:

I think you are using table object which is [Videos]() and you are using this for example in numberOfRowInSection or when making cell settings. So why you dont use this?

You can basically access all properties with using

  print(table[indexPath.row].userName)
  print(table[indexPath.row].userLikes)
  ...
  • Related