Swift-通过P​​FQueryTableViewController创建Twitter或Instagram等个人资料页面

时间:2015-11-02 21:40:28

标签: ios swift parse-platform pfquery

我将一个TableView子类化为PFQueryTableViewController,我想知道如何从Parse中检索用户的库存。我无法接收用户数据并使用我的代码进行显示。我需要在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
    }

1 个答案:

答案 0 :(得分:0)

您永远不会将profilePosts分配给从解析返回的objects。你有两种方法来解决这个问题。一种是坚持使用对象的默认解析实现,只要您需要一个数据库对象,就可以使用objects。另一种方法是坚持使用profilePosts数组并将其分配到objectsDidLoad,这样可以防止您更改除此之外的任何代码。否则,在numberOfRowsInSection等方法中,您需要返回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方法已被修改,以显示您需要做什么来满足您的查询需求。它还会显示那些在遇到问题时如何正确检索对象的人。