2015-10-18 42 views
0

每當我運行我的應用程序,第一次包含tableview視圖加載時,表視圖不會被填充。但是,一旦我切換標籤頁(我正在使用標籤欄控制器),然後切換回來,表視圖就會填充。有沒有人有任何想法如何解決這個問題? 下面是代碼:表視圖不填充視圖第一次加載(Swift2和解析)

import UIKit 
import Parse 

class NotesViewController: UITableViewController { 

var notes = [PFObject]() 

var cacheNotes = Bool() 

@IBOutlet var showContentTableView: UITableView! 

func findNotes() { 

    if let user = PFUser.currentUser()?.username! { 

     let network = Reachability.isConnectedToNetwork() 

     let query = PFQuery(className: "notes") 
     query.whereKey("username", equalTo: user) 

     if network == true { 
      print("connected to network") 
     } else { 
      ("not connected to network") 
      query.fromLocalDatastore() 
     } 

     query.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in 
      if error == nil { 

       self.notes = objects! 

       for object in objects! { 
        if self.cacheNotes == true && network == true {object.pinInBackground()} 
       } 
      } 
     }) 

    } 



    showContentTableView.reloadData() 
} 


override func viewDidLoad() { 
    super.viewDidLoad() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem() 

    if NSUserDefaults.standardUserDefaults().objectForKey("cacheNotes") == nil { 
     NSUserDefaults.standardUserDefaults().setObject(true, forKey: "cacheNotes") 
    } 

} 

override func viewDidAppear(animated: Bool) { 

    cacheNotes = NSUserDefaults.standardUserDefaults().objectForKey("cacheNotes") as! Bool 


    print(cacheNotes) 
    findNotes() 
    showContentTableView.reloadData() 
} 

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 notes.count 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) 
    // Configure the cell... 
    cell.textLabel?.text = notes[indexPath.row]["title"] as? String 

    return cell 
    } 
} 
+1

你需要在'findObjectsInBackgroundWithBlock'的閉包/塊內重新加載表格 – Paulw11

回答

1

當您檢索使用findObjectsInBackgroundWithBlock網絡操作在後臺發生從解析數據,而函數的其餘部分一直在執行。因此,showContentTableView.reloadData()會在數據返回之前立即發生。您需要一次網絡操作完成重裝該表數據,也就是塊/瓶蓋內

func findNotes() { 
    if let user = PFUser.currentUser()?.username! { 

     let network = Reachability.isConnectedToNetwork() 

     let query = PFQuery(className: "notes") 
     query.whereKey("username", equalTo: user) 

     if network == true { 
      print("connected to network") 
     } else { 
      ("not connected to network") 
      query.fromLocalDatastore() 
     } 

     query.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in 
      if error == nil { 

       self.notes = objects! 

       showContentTableView.reloadData() 

       for object in objects! { 
        if self.cacheNotes == true && network == true {object.pinInBackground()} 
       } 
      } 
     }) 
    } 
} 

另外,我建議你從viewWillAppear而不是viewDidAppear稱之爲findNotes - 它會得到一個良好的開端在表格在屏幕上之前填充數據。

+0

我已經提出了建議的調整和它的工作原理。謝謝,並感謝您提醒我解析框架中的異步塊。 – R21