2015-10-18 89 views
0

我试图从Parse显示数据到以下tableView控制器。出于某种原因,数据不会显示在tableView上(即行是空白的)。我不认为从Parse查询的数据被追加到数组中。我想知道我在这里做错了什么。SWIFT:很难在tableView中显示数据

这里的电流输出:

enter image description here

我使用的自定义原型细胞与识别符 “CellTrack” 类 “TrackTableViewCell” 和,如下所示:

enter image description here

enter image description here

这是我在TableViewController文件中的代码:

import UIKit 
import Parse 

class MusicPlaylistTableViewController: UITableViewController { 

var usernames = [String]() 
var songs = [String]() 
var dates = [String]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

} 

override func viewWillAppear(animated: Bool) { 
    var query = PFQuery(className:"PlaylistData") 

    query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in 

     if error == nil { 

      if let objects = objects! as? [PFObject] { 

       self.usernames.removeAll() 
       self.songs.removeAll() 
       self.dates.removeAll() 

       for object in objects { 

        let username = object["username"] as? String 

         self.usernames.append(username!) 
         print("added username") 


        let track = object["song"] as? String 

         self.songs.append(track!) 


        let date = object["createdAt"] as? String 

         self.dates.append(date!) 



        self.tableView.reloadData() 
       } 
      } 

     } else { 

      print(error) 
     } 
    } 
} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 

} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return usernames.count 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("CellTrack", forIndexPath: indexPath) as! TrackTableViewCell 

    cell.username.text = usernames[indexPath.row] 
    cell.songTitle.text = songs[indexPath.row] 
    cell.CreatedOn.text = dates[indexPath.row] 

    return cell 
} 

} 

这里是我在 “TrackTableViewCell.swift” 类代码:

import UIKit 

class TrackTableViewCell: UITableViewCell { 


@IBOutlet weak var songTitle: UILabel! 

@IBOutlet weak var username: UILabel! 

@IBOutlet weak var CreatedOn: UILabel! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

} 
+0

尝试设置断点调试。并告诉我们哪个代码块从未被击中。 – t4nhpt

+0

这很有趣。我在旁边添加了断点:1)query.findObjectsInBackgroundWithBlock {(objects:[PFObject] ?, error:NSError?) - > void in; 2)self.usernames.append(用户名!)。当我选择相关屏幕时,它们都没有被执行。如果这些查询甚至在视图中将会出现? – SB2015

+0

除了已经提供的一些建议之外,还可以将您的调用移动到for循环之外的tableView.reload()中......只有在完成分配tableView的列表后,才应该调用该函数。 – BonanzaDriver

回答

0

执行您在tableView.reloadData()主线程。

dispatch_async(dispatch_get_main_queue(), { 
    self.tableViewCell.reloadData() 
}) 
+1

根据https://parse.com/questions/what-thread-does-findobjectsinbackgroundwithblock-complete-on,Parse回调*在主线程上执行,所以这不是必须的。 –

+0

您能否详细说明“主线程”是什么?我基本上用上面提供的内容替换了之前的内容(即,在添加IF语句之后的“self.tableView.reloadData()”)。 – SB2015

+0

我猜'findObjectsInBackgroundWithBlock'在后台线程中执行,如果你想更新你的UI,你必须在主线程中执行UI代码。但是,正如上面推荐的@MartinR,它已经在主线程上运行了。 – t4nhpt

0

试着做一个警戒让我们来看看这些值是否真的回来作为字符串或不。我的猜测是,创造的价值永远不会回来。试试看,让我知道。

guard let username = object["username"] as? String else { 
    print ("could not get username") 
    } 

    self.usernames.append(username) 
    print("added username") 

    guard let track = object["song"] as? String else { 
    print ("could not get song") 
    return 
    } 
    self.songs.append(track) 

    guard let date = object["createdAt"] as? String else { 
    print ("could not get createdAt") 
    return} 

    self.dates.append(date!) 
0

func dequeueReusableCellWithIdentifier(_ identifier: String) -> UITableViewCell?

Return Value

A UITableViewCell object with the associated identifier or nil if no such object exists in the reusable-cell queue.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    var cell = tableView.dequeueReusableCellWithIdentifier("CellTrack", forIndexPath: indexPath) as! TrackTableViewCell 

    if cell == nil { 
    // create a new cell here 
    cell = TrackTableViewCell(...) 
    } 

    cell.username.text = usernames[indexPath.row] 
    cell.songTitle.text = songs[indexPath.row] 
    cell.CreatedOn.text = dates[indexPath.row] 

    return cell 
}