我正在尝试从parse.com获取一些字符串和一张照片以获取tableview。我有一个这个类的NSObject,还有一个存储它们的对象数组。我试图获取newsPhoto时失败,我可以按正确的顺序获取newsTitle和newsDetail。我想在尝试获取块中的图像时它会丢失它的顺序。有人知道我应该在下面的代码中修改什么来修复它吗?
func getNews(){
let query = PFQuery(className: "bulletinOnParse")
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock {
(allNews: [PFObject]?, error: NSError?) -> Void in
if error == nil {
var duyuru:News
for news in allNews! {
duyuru = News()
let nTitle = news.objectForKey("title") as! String
duyuru.newsTitle = nTitle
let nDetail = news.objectForKey("comment") as! String
duyuru.newsDetail = nDetail
let imageFile = news["newsphoto"] as! PFFile
imageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let image = UIImage(data:imageData)
duyuru.newsPhoto = image!
}
}
}
self.bulletin += [duyuru]
}
} else {
// Log details of the failure
print("\(error!.userInfo)")
}
self.tableView.reloadData()
}
}
下面的cellForRowAtIndexPath方法
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! DuyuruTableViewCell
self.tableView.rowHeight = 100
let cellInfo = bulletin[indexPath.row]
cell.newsTitle.text = cellInfo.newsTitle
cell.news.text = cellInfo.newsDetail
dispatch_async(dispatch_get_main_queue(), {
cell.newsPhoto.image = cellInfo.newsPhoto
})
return cell
}
答案 0 :(得分:0)
以下是我如何解决问题的答案;
首先,我从PFFile对象
创建了一个图像数组var resultUserImageFiles = [PFFile]()
然后我获取名称并在getNews()方法
上添加数组self.resultUserImageFiles.append(news.objectForKey("newsphoto") as! PFFile)
我在cellForRowAtIndexPath方法的下面的方法中得到每张照片。
self.resultUserImageFiles[indexPath.row].getDataInBackgroundWithBlock { (imageData: NSData?, error:NSError?) -> Void in
if error == nil {
let image = UIImage(data: imageData!)
cell.newsPhoto.image = image
}
}