2015-10-18 78 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 
}