2017-03-01 18 views
0

我有一個名爲myArray在firebase數據庫結構中的對象,它有多行字符串作爲[a,b和c]像這樣My DB Structure我該如何顯示這個對象在我的表視圖,當我運行它,它在我的調試來這樣My Debug但它在我的模擬器沒有出現在其安撫這樣My Simulator,我使用SWIFT 3我怎麼能存儲對象有多個字符串從firebase數據庫使用swift3

這是我的代碼:

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? 

} 

回答

1

您的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

+0

我有這條線的錯誤(對於(key,value)arrayList [hh] .myArray)說[[成員'下標'的歧義引用]] –

+0

add'as! [String:String]'到arrayList [hh] .myArray。或者使myArray爲[String:String]。替換行 var myArray:NSArray = []在文本類中爲 var myArray = [String:String]() – sschunara

+0

謝謝,它現在的工作 –

相關問題