[英]SWIFT: Difficultly displaying data in tableView
I am attempting to display data from Parse onto the following tableView controller. 我正在尝试将Parse中的数据显示到以下tableView控制器上。 For some reason, the data is not displaying on the tableView (ie the rows are blank). 由于某种原因,数据未显示在tableView上(即,行为空白)。 I do not think that the data queried from Parse is being appended to the arrays. 我认为从Parse查询的数据不会附加到数组中。 I am wondering what I'm doing wrong here. 我想知道我在做什么错。
Here's the current output: 这是当前的输出:
I am using a custom prototype cell with identifier "CellTrack" class "TrackTableViewCell" and as shown below: 我正在使用具有标识符“ CellTrack”类“ TrackTableViewCell”的自定义原型单元,如下所示:
Here is my code in the TableViewController file: 这是我在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
}
}
And here is my code in the "TrackTableViewCell.swift" class: 这是我在“ 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
}
}
Execute your tableView.reloadData()
in main thread. 在主线程中执行tableView.reloadData()
。
dispatch_async(dispatch_get_main_queue(), {
self.tableViewCell.reloadData()
})
Try doing a guard let to see if those values are actually coming back as string or not. 尝试进行保护,以查看这些值是否实际上以字符串形式返回。 My guess would be that the value for created at never came back. 我的猜测是at创造的价值永远不会回来。 Try it out and let me know. 试试看,让我知道。
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!)
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. 具有关联标识符的UITableViewCell对象;如果在可重用单元格队列中不存在这样的对象,则为nil。
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.