【问题标题】:Swift- Creating a profile page like Twitter or Instagram through PFQueryTableViewControllerSwift - 通过 PFQueryTableViewController 创建个人资料页面,如 Twitter 或 Instagram
【发布时间】:2015-11-02 21:40:27
【问题描述】:

我有一个子类为 PFQueryTableViewController 的 TableView,我想知道如何从 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
    }

【问题讨论】:

    标签: ios swift parse-platform pfquery


    【解决方案1】:

    您永远不会将您的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 方法已被修改以显示您需要做什么来满足您的查询需求。它还向那些来寻找如何在遇到问题时正确检索对象的人展示。

    【讨论】:

    • 当前用户的数据不显示。这如何从 objectId 中检索当前用户对象?
    • 您同时使用objects,它存储在PFTableViewController 上的属性,表示从Parse 检索到的对象,但您也使用profilePosts,这是您认为应该使用的数组用于表示您的 Parse 对象,但您从未实际分配给任何东西。因此,您的选择是使用您已经拥有的属性objects,或者将您的代码更改为仅使用profilePosts,您必须在objectsDidLoad中分配它
    • @Cooni 还更新了我的答案,让您更好地了解我在说什么。
    • 抱歉,我在之前的评论中没有很好地表达我的问题。我理解为什么我需要实施这些解决方案中的任何一个。我担心它会显示当前用户对象吗?我问是因为我有一个 HomeViewController 查询所有用户对象,并且我目前在两个 viewControllers 中使用相同的 parseClassName。所以我只想知道我是否不会在 HomeViewController 中查询所有相同的对象。
    • 是的,您在查询中拥有正确的参数来获取当前用户的对象。其实应该是query.whereKey("username", equalTo: PFUser.currentUser().username)
    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    相关资源
    最近更新 更多