我在从解析中获取用户图像并将其加载到表视图时遇到问题。我认为这与userId / Strings和指针buissness有关!
图片上传很好解析,我可以看到它们,但我似乎无法下载它们。我的代码肯定是问题所在,我无法弄清楚哪个部分。
在解析userId是指向用户类的指针时,所以我非常确定我做错了连接它的所有内容..!我是swift& amp;的新手解析所以我仍然有点难以理解这一切。
以下是我的代码,如果有人能够理解我会喜欢的问题,如果你能告诉我的话那么!!
import UIKit
import Parse
class TimelineTableViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIPopoverPresentationControllerDelegate {
@IBOutlet var feedTableView: UITableView!
var refresher:UIRefreshControl!
var titles = [String]()
var username = [String]()
var imageFiles = [PFFile]()
var users = [String: String]()
override func viewDidLoad() {
super.viewDidLoad()
let query = PFUser.query()
query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let users = objects {
self.titles.removeAll(keepCapacity: true)
self.imageFiles.removeAll(keepCapacity: true)
self.users.removeAll(keepCapacity: true)
self.username.removeAll(keepCapacity: true)
for object in users {
if let user = object as? PFUser {
self.users[user.objectId!] = user.username!
}
}
}
let getFollowedUsersQuery = PFQuery(className: "followers")
getFollowedUsersQuery.whereKey("follower", equalTo: PFUser.currentUser()!)
getFollowedUsersQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
for object in objects {
let followedUser = object ["following"] as! String
let query = PFQuery(className: "Post")
query.whereKey("userId", equalTo: followedUser)
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for object in objects {
self.titles.append(object["title"]! as! String)
self.imageFiles.append(object["imageFile"]! as! PFFile)
self.username.append(self.users[object["userId"] as! String]!)
self.tableView.reloadData()
} }}) } } } })
// Add the refresher
refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "")
refresher.addTarget(self, action: "refreshData", forControlEvents: UIControlEvents.ValueChanged)
// Add the refresher as a sub view on the tableview
self.feedTableView.addSubview(refresher)
}
func refreshData() -> Void {
self.refresher.endRefreshing()}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func blogPost(sender: AnyObject) {
}
@IBAction func CameraPopover(sender: AnyObject) {
self.performSegueWithIdentifier("cameraPopover", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "cameraPopover"
{
let vc = segue.destinationViewController
let controller = vc.popoverPresentationController
if controller != nil
{
controller?.delegate = self
vc.preferredContentSize = CGSizeMake(50, 90)
}
}}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return.None
}
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 username.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let postCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TimelineTableViewCell
imageFiles[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in
if let downloadedImage = UIImage(data: data!) {
postCell.postedImage.image = downloadedImage
}
}
postCell.usernameLabel.text = username[indexPath.row]
postCell.titleLabel.text = titles[indexPath.row]
return postCell
}
}
答案 0 :(得分:1)
您在userId指针中提到的问题是:
let followedUser = object ["following"] as! String
let query = PFQuery(className: "Post")
query.whereKey("userId", equalTo: followedUser)
您提到userId
类的Post
列是User
指针,但是您将其引用为followedUser
的字符串。
关于无法正常显示的图像,在刷新UI之前不会显示它们。您正在使用正确的代码加载
的数据imageFiles[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in
if let downloadedImage = UIImage(data: data!) {
postCell.postedImage.image = downloadedImage
}
但问题是,由于数据是 异步加载 ,因此postCell
将在获取数据之前返回到表中。缺少的关键是使用完成处理程序中的self.tableView.reloadData()
刷新表。
imageFiles[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in
if let downloadedImage = UIImage(data: data!) {
postCell.postedImage.image = downloadedImage
self.tableView.reloadData()
}
或者,我建议您考虑使用PFImageView
框架中的ParseUI
。在完成查询后,您将PFFiles
中的每一个分配给PFImageView
,然后在每个loadInBackground
上调用PFImageView
。数据加载后,PFImageView
类会自动处理更新图像。