我正在尝试将 Parse 中的数据显示到以下 tableView Controller 上。由于某种原因,数据未显示在 tableView 上(即行为空白)。我不认为从 Parse 查询的数据被附加到数组中。我想知道我在这里做错了什么。
这是当前的输出:
我正在使用带有标识符“CellTrack”类“TrackTableViewCell”的自定义原型(prototype)单元,如下所示:
这是我在 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
}
}
最佳答案
在主线程中执行tableView.reloadData()
。
dispatch_async(dispatch_get_main_queue(), {
self.tableViewCell.reloadData()
})
关于ios - SWIFT:很难在 tableView 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33198305/