大约9个小时后,我终于找到了答案。我将在此处发布所有详细信息,以供将来找到此内容的人使用。希望他们不会像我一样挣扎:)
目标是从我的 Parse 类“Post”中加载 2 个图像和 2 个字符串,并将它们显示在 PFTableViewCell 中。这些图像之一来自关系数据,来自“Post”类中的指针。
这是我的“Post”PFObject 类的代码:
import Foundation
import Parse
import ParseUI
import UIKit
class Post: PFObject, PFSubclassing {
@NSManaged var image: PFFile
@NSManaged var short_decription: String?
//1
class func parseClassName() -> String {
return "Post"
}
//2
override class func initialize() {
var onceToken: dispatch_once_t = 0
dispatch_once(&onceToken) {
self.registerSubclass()
}
}
override class func query() -> PFQuery? {
let query = PFQuery(className: Post.parseClassName())
query.includeKey("from_user")
query.orderByDescending("createdAt")
// query.whereKey("early_semester", equalTo: true)
return query
}
init(short_decription: String?) {
super.init()
}
override init() {
super.init()
}
}
这是我的 tableCell 的代码:
import UIKit
import ParseUI
import Parse
class DataCell: PFTableViewCell {
@IBOutlet weak var shortDescriptionLabel: UILabel!
@IBOutlet weak var UserProfilePhoto: PFImageView!
@IBOutlet weak var contentImage: PFImageView!
@IBOutlet weak var userUsername: UILabel!
}
这是我的 PFQueryTableViewController 的代码:
import UIKit
import Parse
import ParseUI
import Foundation
class new: PFQueryTableViewController {
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
loadObjects()
}
//1
override func viewWillAppear(animated: Bool) {
loadObjects()
}
//2
override func queryForTable() -> PFQuery {
let query = Post.query()
return query!
}
//3
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject!) -> PFTableViewCell? {
//4
let cell = tableView.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! DataCell
let PostObject = object as! Post
// assign label in tablecell text from my "Post" class
cell.shortDescriptionLabel.text = PostObject["short_decription"] as? String
cell.userUsername.text = PostObject["user2"] as? String
// loading image saved in my "Post" class
var thumbnail: PFFile = PostObject["fichierimage"] as! PFFile
cell.contentImage.file = thumbnail
cell.contentImage.loadInBackground()
/* loading image that is relational data. My "Post" class has
a pointer column "from_user" that points to the "User" class,
and this is loading the profile photos from the User class.
this is possible because I used "query.includeKey("from_user")" in my query.*/
var profilePhoto: PFFile = PostObject.objectForKey("from_user")!.objectForKey("profilePicture") as! PFFile
cell.UserProfilePhoto.file = profilePhoto
cell.UserProfilePhoto.loadInBackground()
return cell
}
}