2016-08-30 13 views
0

こんにちは、私はスウィフトの初心者です、私は助けが必要です私はfirebaseからデータを取得することができますが、私はtableviewでそれを表示したい私を助けることができますか?これは私のコードです: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) 
    } 

} 
+1

はSOへようこそカスタムブッククラスを持っています。これを見てください:http://stackoverflow.com/help/how-to-ask –

答えて

1

これは、テーブルビューの例である:

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() 
        }) 


      } 
     }) 
    } 

もちろん、私はカスタムセル

+0

私のためには動作しません – KaiTadashi

+0

カスタムブッククラスを作成しましたか? – Carlo

関連する問題