Я пытаюсь заполнить TableView
данными из Firebase в TableViewController
ниже.Методы делегатов UITableViewController, вызываемые до viewDidLoad()
Добавляя операторы печати, я могу видеть, что следующие методы делегата вызывается перед viewDidLoad()
, который где я загрузке мои данные:
func numberOfSectionsInTableView
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
import UIKit import Firebase import SwiftyJSON class MovieTableViewController: UITableViewController { //MARK: Properties var movies = [Movie]() override func viewDidLoad() { self.tableView.delegate = self self.tableView.dataSource = self loadMovies() print("I'm called before delegate methods") print(self.movies.count) super.viewDidLoad() // Use the edit button item provided by the table view controller. navigationItem.leftBarButtonItem = editButtonItem() // 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() } override func viewDidAppear(animated: Bool) { print(movies.count) } func loadMovies() { print("Loading movies") currentUserMovies.observeEventType(FEventType.Value, withBlock: { snapshot in let enumerator = snapshot.children var image = UIImage() while let child = enumerator.nextObject() as? FDataSnapshot { let name = child.key let userRating = child.childSnapshotForPath("User Rating").value as! Int let imdbRating = child.childSnapshotForPath("IMDB Rating").value as! String let plot = child.childSnapshotForPath("Plot").value as! String let imagePath = child.childSnapshotForPath("Image").value as! String if let url = NSURL(string: imagePath) { if let data = NSData(contentsOfURL: url) { image = UIImage(data: data)! } } let movie = Movie(name: name, userRating: userRating, image: image, plot: plot, imdbRating: imdbRating)! self.movies.append(movie) } print(self.movies.count) }) } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { print(self.movies.count) print("tableView returning movie count was called") return self.movies.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { print("This is called too!") let cellIdentifier = "MovieTableViewCell" let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MovieTableViewCell let movie = movies[indexPath.row] cell.movieName.text = movie.name print(movie.name + "name here") cell.movieImage.image = movie.image cell.movieRating.rating = movie.userRating return cell } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() self.tableView.contentInset = UIEdgeInsetsMake(0,0,55,0) } }
В результате табличный вид представляет собой просто пустые строки. Как я могу решить эту проблему?
Убедитесь, чтобы перезагрузить таблицу в самом конце закрытия в '' currentUserMovies.observeEventType' в loadMovies' , в этом случае прямо вне цикла while. – BallpointBen