ios - 如何在 TableView 中显示 Firebase 数据?

标签 ios swift firebase tableview firebase-realtime-database

我需要帮助,我可以从 firebase 获取数据,但我想在 tableview 中显示它,你能帮我吗?这是我的代码:

import UIKit
import Firebase
import FBSDKLoginKit
import FirebaseAuth

class ViewController: UIViewController, FBSDKLoginButtonDelegate {

    let loginButton: FBSDKLoginButton = {
        let button = FBSDKLoginButton()
        button.readPermissions = ["email"]
        return button
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.getDataFireBase()
        view.addSubview(loginButton)
        loginButton.center = CGPointMake(160, 500)
        loginButton.delegate = self
    }
    // get data
    func getDataFireBase() {
        FIRDatabase.database().reference().child("Categories").observeEventType(.ChildAdded, withBlock: { (snapshot) in
            print(snapshot)
            }, withCancelBlock: nil)
    }

}

最佳答案

这是一个表格 View 的例子:

import UIKit
import Firebase


    var bookList = [Book]()
    let ref = FIRDatabase.database().reference().child("Books")


 override func viewDidLoad() {
        super.viewDidLoad()
        retrieveBooks()
    }


    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return self.bookList.count

    }

  override func tableView(tableView: UITableView,
                   cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CellBook",
                                                               forIndexPath: indexPath) as! ListBooksCell

        var book = Book()

        book = bookList[indexPath.row]

        cell.user_name.text = book.user_name
        cell.book_title.text = book.book_title
        cell.book_author.text = book.book_author
        cell.book_price.text = book.book_price! + "€"

        cell.user_name.textColor = UIColor(red: 114 / 255,
                                          green: 114 / 255,
                                          blue: 114 / 255,
                                          alpha: 1.0)
        return cell
    }


 func retrieveBooks(){
        ref.queryOrderedByChild("book_price").observeEventType(.ChildAdded, withBlock: {
        (snapshot) in


            if let dictionary = snapshot.value as? [String:AnyObject]{

                let book = Book()

                book.setValuesForKeysWithDictionary(dictionary)

                    self.bookList.append(book)
                    dispatch_async(dispatch_get_main_queue(), {
                        self.tableView.reloadData()
                    })


            }
        })
    }

显然我有一个自定义单元格和一个自定义Book

关于ios - 如何在 TableView 中显示 Firebase 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39225236/

相关文章:

java - Firebase 实时数据库中的节点是什么?

ios - 在后台检测 iOS 上的屏幕触摸

ios - swiftUI:登录完成后导航到主屏幕。通过单击按钮导航 View

android - FirebaseCrashlytics : Create report HTTP request failed. okhttp3 超时

ios - Swift iOS Firebase - 如何组合 queryOrdered(byChild) 和 GeoFireobserve(.keyEntered) 以同时获取快照和位置结果?

ios - 返回 UINavigationController 时未调用 viewWillAppear

ios - 如何允许用户将照片添加到其他人可以使用 Firebase 查看的应用程序

ios - 页面 View Controller :custom page slider

objective-c - "Incompatible pointer..."将委托(delegate)分配给自己时发出警告

ios - 为什么我向下滚动时看到的单元格多于 numberOfRowInSection 的重新运行值?