2017-04-09 98 views
0

因此,我试图在我的Firebase帐户中使用数据库信息填充我的UITableView。这是在Xcode8,Swift 3中,使用UIKit和我的pod中安装的所有必需的依赖项。使用Firebase数据库信息填充UITableView

内火力地堡的格式如下:

Cell: 
    1: 
     name: "" 
     artist: "" 
     picture: "" 
     url: "" 
    2: 
     name: "" 
     artist: "" 
     picture: "" 
     url: "" 

     Cont... 

这里是我的viewDidLoad()函数内我的代码:我用我的viewDidLoad()功能内的信息硬编码测试,我的表

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.delegate = self 
    tableView.dataSource = self 

    //Set firebase refferences 
    dbRef = FIRDatabase.database().reference() 
    cellRef = dbRef?.child("Cells") 

    cellRef?.observe(FIRDataEventType.childAdded, with: { 

     (snapshot: FIRDataSnapshot) in 

     let artist = snapshot.childSnapshot(forPath: "artist").value as! String 
     let title = snapshot.childSnapshot(forPath: "title").value as! String 
     let picture = snapshot.childSnapshot(forPath: "picture").value as! String 
     let vidURL = snapshot.childSnapshot(forPath: "url").value as! String 

     print("\(artist) \n\(title) \n\(picture) \n\(vidURL)") 

     let youtubeURL = NSURL(string: vidURL) 
     let youtubeRequest = NSMutableURLRequest(url: youtubeURL! as URL) 
     youtubeRequest.setValue("https://www.youtube.com", forHTTPHeaderField: "Referer") 

     let p1 = AddCell(imageURL: picture , 
          videoURL: youtubeRequest, 
          songTitle: title, 
          artistName: artist) 

     self.addCells.append(p1) 

    }) 
} 

注如下。下面的代码完美工作,并在我的视图中提供了一张表格,其中包含适当的信息和视频链接。

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.delegate = self 
    tableView.dataSource = self 

    let youtubeURL = NSURL(string: https://www.youtube.com/embed/Fx-EfjsRcBk) 
    let youtubeRequest = NSMutableURLRequest(url: youtubeURL! as URL) 
    youtubeRequest.setValue("https://www.youtube.com", forHTTPHeaderField: "Referer") 

    let p1 = AddCell(imageURL: http://theshotgunseat.com/wp-content/uploads/2015/03/sam-hunt-2014-promo-650x400.jpg, 
          videoURL: youtubeRequest, 
          songTitle: "Body Like A Back Road", 
          artistName: "Sam Hunt") 

    self.addCells.append(p1) 

正如你可以看到上面的第一个viewDidLoad()功能,我要求打印出我从火力地堡接收信息,我得到这个作为我的项目中的结果:

Sam Hunt 
Body Like A Back Road 
http://theshotgunseat.com/wp-content/uploads/2015/03/sam-hunt-2014-promo-650x400.jpg 
https://www.youtube.com/embed/Fx-EfjsRcBk 
Keith Urban 
The Fighter 
https://pbs.twimg.com/profile_images/714823132851400707/bKM-rUs_.jpg 
https://www.youtube.com/embed/GzT4p-OaJ5c 

正如我确信你可以猜到的,我的问题是,当我试图从Firebase直接填充时运行它时,它不会加载到我的表中,因为当我为一首特定歌曲进行硬编码时,它完美地工作。

任何帮助将是真棒,如果有什么我错过解释,我很抱歉,但请问,所以我可以澄清!

回答

0

我发现我自己的问题。它是那样简单添加

self.tableView.reloadData() 

cellRef?.observe(FIRDataEventType.childAdded, with: { ... 

函数结束

相关问题