[英]How can I display Firebase data in TableView?
I need help I can get data from firebase but I want to display it in tableview can you help me please? 我需要帮助,我可以从firebase中获取数据,但是我想在tableview中显示它,可以帮我吗? This is my code: 这是我的代码:
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)
}
}
This is an example of table 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()
})
}
})
}
Obviously I have a custom cell and a custom Book class 显然我有一个自定义单元格和一个自定义Book类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.