1
我創建了一個配置文件頁面來顯示所有用戶的博客文章。在我的viewController中,我使用了PFQueryTableViewController
來查詢用戶博客。我遇到的問題是當我運行我的應用程序來查看當前用戶博客時,我看到當前用戶首先按創建順序發佈,然後在我看到其他人的博客來自我在Parse中創建的className 。當我在這個頁面上時,這個帖子也在重複。在我的代碼中需要做些什麼才能顯示當前登錄的用戶博客,而不是Parse中的所有帖子?我粘貼了下面的代碼以獲得更好的想法。Swift:試圖在一個頁面中只顯示用戶數據
import UIKit
import Parse
import ParseUI
class ProfileVC: PFQueryTableViewController {
var profilePage: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 = "BlogPost"
self.textKey = "blogging"
self.pullToRefreshEnabled = true
self.objectsPerPage = 200
}
override func viewDidLoad() {
super.viewDidLoad()
let user = PFObject(className:"BlogPost")
user["writer"] = PFUser.currentUser()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func objectsDidLoad(error: NSError?) {
super.objectsDidLoad(error)
profilePage = NSMutableArray(array: objects!)
self.tableView.reloadData()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let query = PFQuery(className: "BlogPost")
if let user = PFUser.currentUser() {
query.whereKey("writer", equalTo: PFUser.currentUser()!.username!)
query.addAscendingOrder("createdAt")
query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil{
for object in objects!{
let blog : PFObject = object as! PFObject
self.profilePage.addObject(blog)
}
let array : NSArray = self.profilePage.reverseObjectEnumerator().allObjects
self.profilePage = NSMutableArray(array: array)
self.tableView.reloadData()
}
}
}
}
// MARK: - Table view data source
override func objectAtIndexPath(indexPath: NSIndexPath!) -> PFObject? {
var obj : PFObject? = nil
if(indexPath.row < self.profilePage.count){
obj = self.profilePage[indexPath.row] as? PFObject
}
return obj
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return profilePage.count
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 0 {
return 110
}
else {
return 90
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath?) -> PFTableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("PCell", forIndexPath: indexPath!) as! ProfileCell
if let object : PFObject = self.profilePage.objectAtIndex(indexPath!.row) as? PFObject {
cell.WritersName.text = object["writer"] as? String
cell.WritersBlog.text = object["Blog"] as? String
let dateCreated = object.createdAt! as NSDate
let date = NSDateFormatter()
dateFormat.dateFormat = "h:mm a"
cell.timeProfile.text = NSString(format: "%@", date.stringFromDate(dateCreated)) as String
}
return cell
}
// }
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
}