gpt4 book ai didi

ios - 第一次加载 View 时不填充 TableView (Swift2 和解析)

转载 作者:行者123 更新时间:2023-11-29 01:33:34 25 4
gpt4 key购买 nike

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

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 从 Parse 检索数据时,网络操作在后台发生,而该函数的其余部分继续执行。因此,showContentTableView.reloadData() 会在数据返回之前立即发生。一旦网络操作完成,即在 block /闭包内,您需要重新加载表数据

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 - 它会在填充数据之前抢先一步该表格在屏幕上。

关于ios - 第一次加载 View 时不填充 TableView (Swift2 和解析),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33203583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com