我有一個從 Firebase 獲取一些資料的 tableView。例如,當用戶點擊一行時,我想獲取字串名稱。
這就是我生成 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)
}
}
})
有了這個,我可以看到是否有人點擊了一行,甚至可以看到哪一行被點擊了。
extension FirstTabSecondViewRight: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("you tapped me!")
print(indexPath.row)
self.performSegue(withIdentifier: "CommentDetail", sender: Any?.self)
}
}
但是我怎樣才能得到例如被點擊行的字串用戶名?
我嘗試使用print(Object),然后它向我顯示了一些字典,但我無法通過說來訪問它們Object[0]
結果print(Object)是這樣的:
["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 ]
uj5u.com熱心網友回復:
我認為您使用table的是物件,[Videos]()并且您正在使用它,例如numberOfRowInSection在進行單元格設定時或在進行單元格設定時。那你為什么不使用這個?
您基本上可以使用 using 訪問所有屬性
print(table[indexPath.row].userName)
print(table[indexPath.row].userLikes)
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/486761.html
標籤:迅速 火力基地 firebase-实时数据库
