ios - SWIFT:很难在 tableView 中显示数据

标签 ios swift xcode7

我正在尝试将 Parse 中的数据显示到以下 tableView Controller 上。由于某种原因,数据未显示在 tableView 上(即行为空白)。我不认为从 Parse 查询的数据被附加到数组中。我想知道我在这里做错了什么。

这是当前的输出:

enter image description here

我正在使用带有标识符“CellTrack”类“TrackTableViewCell”的自定义原型(prototype)单元,如下所示:

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
}

}

最佳答案

在主线程中执行tableView.reloadData()

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

关于ios - SWIFT:很难在 tableView 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33198305/

相关文章:

ios - 如何将两个 .xib 文件添加到两个不同的部分

ios - 导航回来时 UICollection View 没有重新加载?

ios - 测试飞行 2.0.0 : "MyApp" isn't compatible with this device

ios - 为什么我的 UITableView 不再工作了?

swift - 是否有适用于 Xcode7 的 servicestack swift 插件的更新版本?

ios - CKModifyRecordsOperation 完成 Swift 2.0

ios - 在双组件 pickerView 中查找两个变量

objective-c - 存储大量键值对的正确解决方案是什么? NSDictionary 与 DB

swift - OS X。如何使用参数运行控制台应用程序?

swift - 在 Swift 3 中重复 Process() n 次