[英]How can I store object has a multiple strings from firebase database using swift3
我在firebase數據庫結構中有一個名為myArray的對象,它具有像[a,b和c]這樣的多行字符串 如何在表格視圖中顯示該對象,當我運行該對象時,它會像這樣在My Debug中顯示
但它沒有出現在我的模擬器中
,我正在使用swift 3
這是我的代碼:
import UIKit
import Firebase
class TestTable: UITableViewController {
var arrayList = [test]()
var Ref = FIRDatabaseReference()
override func viewDidLoad() {
super.viewDidLoad()
Ref=FIRDatabase.database().reference()
Ref.child("Users").child("Test").observe(.childAdded ,with: { (snapshot) in
if let User = snapshot.value as? [String : AnyObject]{
let Add = test()
Add.setValuesForKeys(User)
self.arrayList.append(Add)
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\(snapshot.value)")
self.tableView.reloadData()
}
})
}
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 {
// #warning Incomplete implementation, return the number of sections
return arrayList.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return arrayList[section].myArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as!TestTableCell
let hh = indexPath.section
cell.myTestView.text = (arrayList[hh].myArray as AnyObject) as? String
cell.titleText.text = arrayList[hh].title
return cell
}
}
這是我的課:
class test : NSObject {
var myArray : NSArray = []
var title : String?
}
您的myArray不是字符串數組,而是字典。 因此,如果要在cell.myTestView.text中使用a,b和c的值。 然后,您需要運行循環以獲取cell.myTestView.text中的所有a,b和c。
var string = ""
for (key, value) in arrayList[hh].myArray
{
print("\(key) -> \(value)")
string = string + "\n\(key) -> \(value)"
}
cell.myTestView.text = string
更新您的行cell.myTestView.text = (arrayList[hh].myArray as AnyObject) as? String
具有上述代碼的cell.myTestView.text = (arrayList[hh].myArray as AnyObject) as? String
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.