繁体   English   中英

我如何使用swift3从Firebase数据库中存储具有多个字符串的对象

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM