我有和這裏幾乎相同的問題:Firebase get child ID swift ios,並且與此問題Retrieve randomly generated child ID from Firebase略有不同。使用UITableViewController從Firebase獲取當前子ID從
我有一個UITableViewController用戶可以創建自己的渠道。每個人都可以看到這些頻道,每個人都可以點擊其中一個頻道,並進入新視角。現在,當用戶點擊任何這些單元格時,我想獲取當前的頻道ID,用戶剛剛點擊。
現在我的問題是:我怎樣才能像電流ID(-KdD6 [...] Q0Q)?問題2的答案會爲房間的創建者做訣竅,但由於他們沒有創建密鑰而不能被其他用戶使用。可以使用問題1的答案,但到目前爲止,它只是循環遍歷每個隨機密鑰。但是我需要與用戶當前所在頻道相關的密鑰。下面是一些代碼,用於說明我現在如何製作頻道。提前致謝。編輯:這是我的所有代碼。在didSelectRow函數中,我現在得到一個錯誤「[Channel]的值沒有成員'name'」。
import UIKit
import Firebase
import Foundation
enum Section: Int {
case createNewChannelSection = 0
case currentChannelsSection
}
extension multiplayerChannelView: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
}
class multiplayerChannelView: UITableViewController {
// MARK: Properties
var channels: [Channel] = []
let searchController = UISearchController(searchResultsController: nil)
var filteredChannels = [Channel]()
private lazy var channelRef: FIRDatabaseReference = FIRDatabase.database().reference().child("channels")
private var channelRefHandle: FIRDatabaseHandle?
var senderDisplayName: String?
var newChannelTextField: UITextField?
override func viewDidLoad() {
super.viewDidLoad()
title = "Rooms"
observeChannels()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
}
deinit {
if let refHandle = channelRefHandle {
channelRef.removeObserver(withHandle: refHandle)
}
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredChannels = channels.filter { Channel in
return (Channel.name.lowercased().range(of: searchText.lowercased()) != nil)
}
tableView.reloadData()
}
@IBAction func createChannel(_ sender: AnyObject) {
if let name = newChannelTextField?.text {
let newChannelRef = channelRef.childByAutoId()
let channelItem = [ // 3
"name": name
]
newChannelRef.setValue(channelItem)
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
private func observeChannels() {
channelRefHandle = channelRef.observe(.childAdded, with: { (snapshot) -> Void in // 1
let channelData = snapshot.value as! Dictionary<String, AnyObject> // 2
let id = snapshot.key
if let name = channelData["name"] as! String!, name.characters.count > 0 { // 3
self.channels.append(Channel(id: id, name: name))
self.tableView.reloadData()
} else {
print("Error! Could not decode channel data")
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let currentSection: Section = Section(rawValue: section) {
switch currentSection {
case .createNewChannelSection:
return 1
case .currentChannelsSection:
if searchController.isActive && searchController.searchBar.text != "" {
return filteredChannels.count
}
else{
return channels.count
}
}
} else {
return 0
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let reuseIdentifier = (indexPath as NSIndexPath).section == Section.createNewChannelSection.rawValue ? "NewChannel" : "ExistingChannel"
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
if (indexPath as NSIndexPath).section == Section.createNewChannelSection.rawValue {
if let createNewChannelCell = cell as? CreateChannelCell {
newChannelTextField = createNewChannelCell.newChannelNameField
}
} else if (indexPath as NSIndexPath).section == Section.currentChannelsSection.rawValue {
if searchController.isActive && searchController.searchBar.text != "" {
cell.textLabel?.text = filteredChannels[(indexPath as NSIndexPath).row].name
} else {
cell.textLabel?.text = channels[(indexPath as NSIndexPath).row].name
}
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == Section.currentChannelsSection.rawValue {
var channel = channels
if searchController.isActive && searchController.searchBar.text != "" {
channel = [filteredChannels[(indexPath as NSIndexPath).row]]
channelRef.queryOrdered(byChild: "name").queryEqual(toValue: channel.name).observe(.childAdded, with: {snapshot in
let currentID = snapshot.key
print(currentID)
})
}
else
{
channel = [channels[(indexPath as NSIndexPath).row]]
channelRef.queryOrdered(byChild: "name").queryEqual(toValue: channel.name).observe(.childAdded, with: {snapshot in
let currentID = snapshot.key
print(currentID)
})
}
self.performSegue(withIdentifier: "ShowChannel", sender: channel)
}
}
}
internal class Channel {
internal let id: String
internal let name: String
init(id: String, name: String) {
self.id = id
self.name = name
}
}
編輯2:我讀有關的賽格瑞準備,但是這不過將無法正確執行:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if let channel = sender as? Channel {
let chatVc = segue.destination as! channelMultiplayerViewController
chatVc.channel = channel
chatVc.channelRef = channelRef.child(channel.id)
}
}
爲賽格瑞函數執行的準備,但如果讓信道=發送者作爲?頻道不是,因爲它是零。
顯示您身在何處頻道添加對象的代碼也有設置通道對象的ID屬性,當你設置它的名字,如果你已經設置比你想這個頻道ID傳遞到新的屏幕ShowChannel? –
我增加了更多的代碼,現在你可以看到我正在做什麼。我只想要檢索房間的ID,不僅是爲了創建者,而且也是爲了那些想加入這個房間的人。 – Petravd1994