代码之家  ›  专栏  ›  技术社区  ›  Christopher Lowiec

Firebase将阵列数据导入tableview

  •  0
  • Christopher Lowiec  · 技术社区  · 6 年前

    我在VC中有tableview,我想从当前配方中导入“详细”项目:

    Firebase entries

    到tableView-每个到一个单独的单元格。

    class RecipiesModel {
    
        var title: String?
        var desc: String?
        var detail: Array<Any>?
    
        init(title: String?, desc: String?, detail: Array<Any>?){
            self.title = title
            self.desc = desc
            self.detail = detail
        }
    }
    

    我在VC中的代码:

    import UIKit
    import FirebaseDatabase
    
    class DescriptionViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
        var textInput: String = ""
        var descInput: String = ""
    
        var ref:DatabaseReference!
        var recipiesList = [RecipiesModel]()
    
        @IBOutlet weak var tableView: UITableView!
        @IBOutlet weak var titleLabelDesc: UILabel!
        @IBOutlet weak var descriptionLabelDesc: UILabel!
        @IBOutlet weak var imageView: UIImageView!
        @IBOutlet weak var tabBarView: UIView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            UIApplication.shared.statusBarStyle = .lightContent
    
            tableView.delegate = self
            tableView.dataSource = self
    
            customUIView()
    
            titleLabelDesc.text = textInput
            descriptionLabelDesc.text = descInput
    
            loadList()
        }
    
        //Database
    
        func loadList() {
            ref = Database.database().reference()
            ref.child("Recipies").observe(.childAdded, with: { (snapshot) in
                let results = snapshot.value as? [String : AnyObject]
                let title = results?["Recipies title"]
                let desc = results?["Recipies description"]
                let detail = results?["Detail"]
                let myRecipies = RecipiesModel(title: title as! String?, desc: desc as! String?, detail: detail as! Array<Any>?)
                self.recipiesList.append(myRecipies)
    
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            })
        }
    
        func customUIView() {
            tabBarView.layer.shadowColor = UIColor.lightGray.cgColor
            tabBarView.layer.shadowOpacity = 1
            tabBarView.layer.shadowOffset = CGSize.zero
            tabBarView.layer.shadowRadius = 3
        }
    
        @IBAction func dismissButton(_ sender: Any) {
            dismiss(animated: true, completion: nil)
        }
    
        //TableView
    
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return recipiesList.count
        }
    
        public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cellDescription") as! TableViewCellDescription
    
            let recipies = recipiesList[indexPath.row]
            cell.recipiesModuleLabel.text = recipies.detail?.description
    
            return cell
        }
    }
    

    此时,结果是:

    Table View Entries

    有什么想法吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Ankit Jayaswal    6 年前

    在您的情况下,您希望在不同的行中显示详细信息项,而您有 RecipiesModel .

    var recipiesList = [RecipiesModel]()
    

    如果你能代表每个 modal object of array 作为一个部分和 details 对象作为其行。您可以这样做:

    // TableView datasource and delegate methods
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return recipiesList.count
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let modal = recipiesList[section]
        return "\(modal.title ?? ""): \(modal.desc ?? "")"
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return recipiesList[section].detail?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellDescription") as! TableViewCellDescription
        if let detailName = recipiesList[indexPath.section].detail?[indexPath.row] as? String {
            cell.recipiesModuleLabel.text = detailName
        } else {
            cell.recipiesModuleLabel.text = ""
        }
        return cell
    }