2015-05-21 103 views
0

我想查詢使用解析將字符串添加到數組。然後我想把這些字符串放到我的UITableView的單元格中。但是,每次運行應用程序時,似乎都沒有出現在我的桌面上。這裏是我的代碼,如果有人可以幫助解釋一些原因,它可能不會出現UITableView Swift問題

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

@IBOutlet weak var tableView: UITableView! 


var friendsArray: [String] = [] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 

    var usrname = currentUser?.username 
    var query = PFQuery(className:"Relation") 
    query.whereKey("Sender", equalTo : usrname!) 
    query.findObjectsInBackgroundWithBlock { 
     (objects: [AnyObject]?, error: NSError?) -> Void in 

     if error == nil { 
      // The find succeeded. 
      //println("Successfully retrieved \(objects) scores.") 
      // Do something with the found objects 
      if let objects = objects as? [PFObject] { 
       for object in objects { 
        var friendName = object["Friend"] as! String 
        println(friendName) 
        self.friendsArray.append(friendName) 
       } 
      } 
     } 
     else { 
      // Log details of the failure 
      println("Error: \(error!) \(error!.userInfo!)") 
     } 
    } 


} 


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return friendsArray.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell 

    cell.textLabel?.text = self.friendsArray[indexPath.row] 

    return cell 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

} 

回答

0

當你在後臺調用findObjectsInBackgroundWithBlock它做什麼它的名字一樣,特別是它的運行。

同時,它在後臺運行時,表視圖代碼繼續在前臺運行,並行運行。

所以你viewDidLoad功能將退出並numberOfRowsInSection將在下面叫,但當時如果findObjectsInBackgroundWithBlock尚未完成,然後friendsArray.count爲0。

+0

那麼我能做些什麼來解決這個問題? – Justin

+0

與昨天的同一問題類似。他們在異地使用異步代碼。 http://stackoverflow.com/questions/30361517/asynchronous-tableviewcell-data/30361658#30361658。原則和解決方案選項相似。 – Gruntcakes

+0

我很困惑,我可以確保Im在ViewdidLoad之後運行CellforRowAtIndexPath函數,因爲我認爲ViewDidLoad或者甚至是ViewDidAppear都會先發生 – Justin

1

你需要調用[self.tableView reloadData];中的完成塊撥打電話findObjectsInBackground:

0

該應用程序不知道您的數據將何時返回,因此您必須在成功接收數據後明確刷新UITableView。使用[self.tableView reloadData]稍後重新加載UITableView

UITableView將只加載一次,當UIViewController被加載。除非,你已經有數據加載時的時間UIViewController。否則,第一次的行數將爲0,並且不會調用委託方法cellForRowAtIndexPath

findObjectsInBackgroundWithBlock是您查看文檔時的異步調用。它不像它的字面含義,它在後臺運行。這意味着它不會阻塞當前線程,以便用戶仍然可以與應用程序進行交互。

+0

@Kasik異步意味着它不會等待響應返回到當前線程。這並不意味着它被分派到另一個線程。 –

-1

正如mentionded由你需要調用reloadData另一條線索,但需要將其儘快

dispatch_async(dispatch_get_main_queue()) {self.tableView.reloadData()} 
+0

他沒有將該代碼塊分派給另一個隊列,它只是在主線程上運行。所以,'self.tableView.reloaddata()'就夠了 –

+0

@LucasHuang我不知道PFQuery,但findInBackground對我來說聽起來不是主線程。它可以通過回調在主線程上執行的方式實現,但這是不尋常的。另一方面,使用dispatch_async幾乎是免費的,它不會讓認爲更糟糕。 – Kasik

+0

他們在主線程中始終執行的原因是因爲開發人員可以根據自己的意願將其分派到另一個隊列中。如果Parse開發人員將其放入其他隊列中,則不是開發人員要控制它。我不認爲這是他們的風格。 –

0

調用主線程看到結果在您的if error == nil{...}語句的結束,加載表格數據,如這樣的:

if error == nil{ 
    //All that other stuff... 
    tableView.reloadData() 
} 

另外,請確保您的tableViewdelegatedatasource連接到您的視圖控制器。這可能需要把這個在viewDidLoad

tableView.delegate = self 
tableView.datasource = self 

我也覺得奇怪,你tableView被聲明爲weak,但可能是不相關的。

嘗試這些事情,讓我們知道如果你仍然有麻煩。祝你好運! :)

+0

這是'弱',因爲它是一個IBOutlet,它直接從他的xib文件中引用 –

0

只是試試這些東西。它會幫助你 之後,你得到元素的數組重新加載uitableView。 如果你用故事板檢查數據源出口 它,你沒有用故事板或XIB,那麼你在編碼設置數據源wisse

0

嗨,你可以使用這樣

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

{ 


    let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell 


    cell.textLabel?.text = app.str[indexPath.row] 




    print(" cell \(cell.textLabel?.text!)") 


    return cell 


} 

TableOutLet.reloadData()