gpt4 book ai didi

ios - Swift-通过 PFQueryTableViewController 创建 Twitter 或 Instagram 等个人资料页面

转载 作者:行者123 更新时间:2023-11-30 13:57:03 26 4
gpt4 key购买 nike

我有一个 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
}

最佳答案

您永远不会将您的profilePosts分配给从解析返回的对象。您有两种选择来解决此问题。一种是坚持使用对象的默认解析实现,这样您就可以在需要数据库对象之一时使用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 方法已被修改以显示您需要执行哪些操作才能满足您的查询需求。它还确实向那些寻求如何在遇到问题时正确检索对象的人展示。

关于ios - Swift-通过 PFQueryTableViewController 创建 Twitter 或 Instagram 等个人资料页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33487474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com