我试图从Parse显示数据到以下tableView控制器。出于某种原因,数据不会显示在tableView上(即行是空白的)。我不认为从Parse查询的数据被追加到数组中。我想知道我在这里做错了什么。SWIFT:很难在tableView中显示数据
这里的电流输出:
我使用的自定义原型细胞与识别符 “CellTrack” 类 “TrackTableViewCell” 和,如下所示:
这是我在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
}
}
尝试设置断点调试。并告诉我们哪个代码块从未被击中。 – t4nhpt
这很有趣。我在旁边添加了断点:1)query.findObjectsInBackgroundWithBlock {(objects:[PFObject] ?, error:NSError?) - > void in; 2)self.usernames.append(用户名!)。当我选择相关屏幕时,它们都没有被执行。如果这些查询甚至在视图中将会出现? – SB2015
除了已经提供的一些建议之外,还可以将您的调用移动到for循环之外的tableView.reload()中......只有在完成分配tableView的列表后,才应该调用该函数。 – BonanzaDriver