Swift- 通过 PFQueryTableViewController 创建像 Twitter 或 Instagram 这样的个人资料页面

Swift- Creating a profile page like Twitter or Instagram through PFQueryTableViewController

我有一个 TableView 子类化为 PFQueryTableViewController,我想知道如何从 Parse 中检索用户 post 的清单。我在接收用户数据和使用我的代码显示时遇到问题。是否需要在我的 queryForTable 中编辑某些内容、更改解析或更改我的代码?

这是我的 ProfileViewController:

import UIKit
import Parse
import ParseUI

class ProfileViewController: PFQueryTableViewController{

    var profilePosts:NSMutableArray! = NSMutableArray()

    override init(style: UITableViewStyle, className: String!) {
        super.init(style: style, className: className)
    }


    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.parseClassName = "Test"
        self.textKey = "postMessage"
        self.pullToRefreshEnabled = true
        self.objectsPerPage = 200

    }


    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func queryForTable() -> PFQuery {
        let query = PFQuery(className: "Test")
        query.whereKey("username", equalTo: PFUser.currentUser()!)
        query.limit = 200;

        return query
    }

    // MARK: - Table view data source

    override func objectAtIndexPath(indexPath: NSIndexPath!) -> PFObject? {
        var obj : PFObject? = nil
        if(indexPath.row < self.objects!.count){
            obj = self.objects![indexPath.row] as? PFObject
        }

        return obj
    }

    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 profilePosts.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath?, object:PFObject!) -> PFTableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("PCell", forIndexPath: indexPath!) as! ProfileCell

        if let profilePost : PFObject = self.profilePosts.objectAtIndex(indexPath!.row) as! PFObject {

            cell.NameProfile.text = object["account"] as? String
            cell.messageProfile.text = object["postMessage"] as? String
            let dateUpdated = object.createdAt! as NSDate
            let dateFormat = NSDateFormatter()
            dateFormat.dateFormat = "MMM d, h:mm a"

            cell.timeProfile.text =  NSString(format: "%@", dateFormat.stringFromDate(dateUpdated)) as String
            cell.messageProfile.numberOfLines = 0
            let likeScore = object[("count")] as! Int
            cell.likeScoreProfile.text = "\(likeScore)"
            let replyscore = object["replies"] as! Int
            cell.replyScoreProfile.text = "\(replyscore) replies"
            cell.messageProfile.text = profilePost.objectForKey("postMessage") as? String
            if let profilePic = object["profilePhoto"] as? PFFile {
                let imageView = cell.profilePhoto as PFImageView

                imageView.image = UIImage(named: "profileImg")
                imageView.file = profilePic

                imageView.loadInBackground(nil) 
            }


        return cell
    }

您永远不会将 profilePosts 分配给从解析中 return 编辑的 objects。您有两种选择来解决这个问题。一种是只坚持对象的默认解析实现,它允许您在需要数据库对象之一时使用 objects。另一种方法是坚持使用 profilePosts 数组并将其分配到 objectsDidLoad 中,这样您就不必更改除此之外的任何代码。否则在像 numberOfRowsInSection 这样的方法中,您需要 return objects!.count 来代替。

如果您决定使用自己的 profilePosts 数组,您需要这样做:

import UIKit
import Parse
import ParseUI

class ProfileViewController: PFQueryTableViewController{

var profilePosts:NSMutableArray! = NSMutableArray()

override init(style: UITableViewStyle, className: String!) {
    super.init(style: style, className: className)
}


required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    self.parseClassName = "Test"
    self.textKey = "postMessage"
    self.pullToRefreshEnabled = true
    self.objectsPerPage = 200

}


override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func objectsDidLoad(error: NSError?) {
    super.objectsDidLoad(error)
    profilePosts = NSMutableArray(array: objects!)
    self.tableView.reloadData()
}

override func queryForTable() -> PFQuery {
    let query = PFQuery(className: "Test")
    query.whereKey("username", equalTo: PFUser.currentUser()!.username)
    query.limit = 200;

    return query
}

// MARK: - Table view data source

override func objectAtIndexPath(indexPath: NSIndexPath!) -> PFObject? {
    var obj : PFObject? = nil
    if(indexPath.row < self.profilePosts.count){
        obj = self.profilePosts[indexPath.row] as? PFObject
    }

    return obj
}

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 profilePosts.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath?) -> PFTableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("PCell", forIndexPath: indexPath!) as! ProfileCell

    if let object : PFObject = self.profilePosts.objectAtIndex(indexPath!.row) as? PFObject {

        cell.NameProfile.text = object["account"] as? String
        cell.messageProfile.text = object["postMessage"] as? String
        let dateUpdated = object.createdAt! as NSDate
        let dateFormat = NSDateFormatter()
        dateFormat.dateFormat = "MMM d, h:mm a"

        cell.timeProfile.text =  NSString(format: "%@", dateFormat.stringFromDate(dateUpdated)) as String
        cell.messageProfile.numberOfLines = 0
        let likeScore = object[("count")] as! Int
        cell.likeScoreProfile.text = "\(likeScore)"
        let replyscore = object["replies"] as! Int
        cell.replyScoreProfile.text = "\(replyscore) replies"
        cell.messageProfile.text = profilePost.objectForKey("postMessage") as? String
        if let profilePic = object["profilePhoto"] as? PFFile {
            let imageView = cell.profilePhoto as PFImageView

            imageView.image = UIImage(named: "profileImg")
            imageView.file = profilePic

            imageView.loadInBackground(nil) 
        }


    return cell
}

请注意我如何更改 cellForRowAtIndexPath 以使用您从 profilePosts 获得的对象,然后我如何覆盖 objectsDidLoad 并分配 PFQueryTableView objects 属性 到您的 profilePosts 数组。您可能想要这样做的一个原因是,通过拥有自己的 Parse 对象托管副本,您可以执行插入或删除动画等操作,这是常规 objects 属性 不允许您执行的操作.希望这对您有所帮助,如果您有更多问题,请告诉我。

编辑 2 根据您对更具体问题的评论,我建议您编辑原始问题以指定您询问的是如何仅获取当前用户的对象,而不是询问要使用的实现。然而,我将上面的代码留在那儿,因为 queryForTable 方法已被修改以显示您需要做什么才能满足您的查询需求。它还确实展示了那些前来寻找如何在遇到问题时正确检索对象的人。