每當我運行我的應用程序,第一次包含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
}
}
你需要在'findObjectsInBackgroundWithBlock'的閉包/塊內重新加載表格 – Paulw11