Swift:numberOfRowsInSection和cellForRowAt没有被调用

时间:2018-07-12 16:44:36

标签: ios swift uitableview

我一直试图显示来自Firebase的数据并显示在tableView中。数据已正确提取但未显示。除了下面提到的代码,我已经为表添加了dataSource和Delegate以及单元格的标识符。

这是 HomeTableViewController

import UIKit
import FirebaseDatabase
import Firebase

class HomeTableViewController: UITableViewController {

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    print(artists.count)
    return artists.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomeTableViewCell
    print("Here")
    let artist = artists[indexPath.row]
    cell.artist = artist
    return cell
}
let artistRef = Database.database().reference().child("Fund")
var artists = [ArtistList]()

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    artistRef.observe(.value, with: { (snapshot) in
        self.artists.removeAll()

        for child in snapshot.children {
            let childSnapshot = child as! DataSnapshot

            let artist = ArtistList(snapshot: childSnapshot)
            print(artist)

            self.artists.insert(artist, at: 0)
        }

        self.tableView.reloadData()
    })

}
override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.isHidden = false
    self.navigationItem.hidesBackButton = true
    title = "Artcall"

    self.tableView.estimatedRowHeight = 290.0
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.reloadData()
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem
}



}

我的 HomeTableViewCell 的主要代码是

var artist: ArtistList!{
    didSet
    {
        let dateString:String = String(format: "%@", artist.date as CVarArg)
        labelDate.text = dateString
        labelName.text = artist.name
        labelLocation.text = artist.city

    }
}

我的模型代码 ArtistList 在这里

class ArtistList
{
var name: String = ""
var city: String = ""
var date: Int64 = 0
let ref: DatabaseReference!

init(snapshot: DataSnapshot) {
    ref = snapshot.ref
    if let value = snapshot.value as? [String : Any] {
        name = value["artist_name"] as! String
        city = value["perf_location"] as! String
        date = value["target_date"] as! Int64
    }

}
}

1 个答案:

答案 0 :(得分:0)

如果在异步函数中调用UI更新,则需要在主线程上进行UI更新,因此请尝试将tableview重新加载放入主队列,如下所示:

override func viewDidAppear(_ animated: Bool) {
  super.viewDidAppear(animated)

  artistRef.observe(.value, with: { (snapshot) in
    self.artists.removeAll()

    for child in snapshot.children {
        let childSnapshot = child as! DataSnapshot

        let artist = ArtistList(snapshot: childSnapshot)
        print(artist)

        self.artists.insert(artist, at: 0)
    }

    //Print the artists array to verify it is definitely populated
    print(self.artists)

    //Reload tableview on the main queue
    DispatchQueue.main.async {
      self.tableView.reloadData()
    }

  })

}