在第一次加载视图时不填充表视图(Swift2和解析)

时间:2015-10-18 21:40:32

标签: ios uitableview parse-platform swift2

每当我运行我的应用程序时,第一次加载包含tableview的视图时,表视图就不会被填充。但是一旦我切换标签(我正在使用标签栏控制器),然后切换回来,表视图就会被填充。有没有人有任何想法如何解决这个问题? 这是代码:

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 个答案:

答案 0 :(得分:1)

当您使用findObjectsInBackgroundWithBlock从Parse检索数据时,网络操作将在后台进行,而其余功能将继续执行。因此,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()}
                }
            }
        })
    }
}

此外,我建议您从findNotes而不是viewWillAppear致电viewDidAppear - 它会在表格在屏幕上之前填充数据。