Population UITableView每行都有多个值

时间:2018-04-16 19:18:37

标签: ios arrays swift firebase firebase-realtime-database

我正在尝试创建一个UITableView来显示商店和每一行的列表,其中包含商店名称和商店类型。我目前将数据存储在以下层次结构enter image description here

然而,无论我尝试什么,我仍然无法让我的UITableView显示列表,所以任何帮助都非常感激。这是我的代码:

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase



class HomeViewController: UIViewController, UITableViewDataSource , UITableViewDelegate  {


struct shopStruct {
    let name: String!
    let type : String!

}


@IBOutlet weak var homeTableView: UITableView!
var databaseRef: DatabaseReference!
 var shops = [shopStruct]()


override func viewDidLoad() {
    super.viewDidLoad()

        databaseRef = Database.database().reference()
        databaseRef.child("shops").observe(.childAdded, with: { (snapshot) in

        if let valueDictionary = snapshot.value as? [AnyHashable:String]
        {
            let name = valueDictionary["name"] as! String
            let type = valueDictionary["type"] as! String
            self.shops.insert(shopStruct(name:name, type:type) at: 0)
            print("\(name) - \(type)")
            DispatchQueue.main.async {
                self.homeTableView.reloadData()

            }
        }
    })
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return shops.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    let label1 = cell.viewWithTag(1) as! UILabel
    label1.text = shops[indexPath.row].name

    let label2 = cell.viewWithTag(2) as! UILabel
    label2.text = shops[indexPath.row].type


    return cell
}

}

非常感谢,我将永远感激任何帮助。

0 个答案:

没有答案