Swift 为什么从 Parse 中检索数据时我必须按两次按钮才能在 tableview 中加载数据
Swift why when Retrieving data from Parse I have to press button twice to load data in tableview
下面是我的代码,它从 Parse 服务器 class 检索图像并将其加载到表视图中。
当我按 GetAlbum UI 按钮显示数据时,我必须按两次才能成功在表格视图中显示数据,我不知道是什么原因。请帮助(PS:我是swift的新手)
var Saveddata: [UIImage] = []
@IBAction func getAlbum(_ sender: AnyObject) {
if (self.textEntercode.text == "")
{
let alert = UIAlertController(title: "No Entered value", message: "Please enter value", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil));
//event handler with closure
present(alert, animated: true, completion: nil);
}
else {
//-------------Get Data from Parse Server-------------------
var photo = textEntercode.text
let query = PFQuery(className: "Photostable10")
query.whereKey("photo", equalTo: photo)
//query.whereKey("Score", greaterThan: 1000)
query.findObjectsInBackground(block: { (objects : [PFObject]?, error: Error?) -> Void in
if error == nil {
for object in objects! {
if let userPicture = object.value(forKey: "photo") {
let userPicture = object.value(forKey: "photo")! as! PFFile
userPicture.getDataInBackground({ (imageData: Data?, error: Error?) -> Void in
let image = UIImage(data: imageData!)
if image != nil {
self.Saveddata.append(image!)
}
})
}
}
}
})
self.tableview.reloadData()
} //else close
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell")!
cell.textLabel!.text = "Post"
cell.imageView?.image = Saveddata[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.Saveddata.count
}
您对 self.tableView.reloadData()
的调用在错误的位置,并且是从错误的队列中调用的。您需要在主队列上调用它,并且需要在更新数据模型后在查询完成处理程序中调用它。
query.findObjectsInBackground(block: { (objects : [PFObject]?, error: Error?) -> Void in
if error == nil {
for object in objects! {
if let userPicture = object.value(forKey: "photo") {
let userPicture = object.value(forKey: "photo")! as! PFFile
userPicture.getDataInBackground({ (imageData: Data?, error: Error?) -> Void in
let image = UIImage(data: imageData!)
if image != nil {
self.Saveddata.append(image!)
}
})
}
}
DispatchQueue.main.async {
self.tableview.reloadData()
}
}
})
下面是我的代码,它从 Parse 服务器 class 检索图像并将其加载到表视图中。 当我按 GetAlbum UI 按钮显示数据时,我必须按两次才能成功在表格视图中显示数据,我不知道是什么原因。请帮助(PS:我是swift的新手)
var Saveddata: [UIImage] = []
@IBAction func getAlbum(_ sender: AnyObject) {
if (self.textEntercode.text == "")
{
let alert = UIAlertController(title: "No Entered value", message: "Please enter value", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil));
//event handler with closure
present(alert, animated: true, completion: nil);
}
else {
//-------------Get Data from Parse Server-------------------
var photo = textEntercode.text
let query = PFQuery(className: "Photostable10")
query.whereKey("photo", equalTo: photo)
//query.whereKey("Score", greaterThan: 1000)
query.findObjectsInBackground(block: { (objects : [PFObject]?, error: Error?) -> Void in
if error == nil {
for object in objects! {
if let userPicture = object.value(forKey: "photo") {
let userPicture = object.value(forKey: "photo")! as! PFFile
userPicture.getDataInBackground({ (imageData: Data?, error: Error?) -> Void in
let image = UIImage(data: imageData!)
if image != nil {
self.Saveddata.append(image!)
}
})
}
}
}
})
self.tableview.reloadData()
} //else close
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell")!
cell.textLabel!.text = "Post"
cell.imageView?.image = Saveddata[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.Saveddata.count
}
您对 self.tableView.reloadData()
的调用在错误的位置,并且是从错误的队列中调用的。您需要在主队列上调用它,并且需要在更新数据模型后在查询完成处理程序中调用它。
query.findObjectsInBackground(block: { (objects : [PFObject]?, error: Error?) -> Void in
if error == nil {
for object in objects! {
if let userPicture = object.value(forKey: "photo") {
let userPicture = object.value(forKey: "photo")! as! PFFile
userPicture.getDataInBackground({ (imageData: Data?, error: Error?) -> Void in
let image = UIImage(data: imageData!)
if image != nil {
self.Saveddata.append(image!)
}
})
}
}
DispatchQueue.main.async {
self.tableview.reloadData()
}
}
})