看不到我在 Parse 中发布的消息
Can't see the messages I'm posting in Parse
我正在创建一个 yik yak 克隆,我似乎无法在 Parse 的 textField(string) 中看到我 post 的消息。我在我的代码中做错了什么没有让它显示在 Parse 上吗?
@IBAction func postPressed(sender: AnyObject) {
if(currLocation != nil){
let testObj = PFObject(className: "BubbleTest")
testObj["userName"] = PFUser.currentUser()?.username
//testObj["profileName"] = PFUser.valueForKey("profileName") as! String
//testObj["photo"] = PFUser.currentUser()?.valueForKey("photo") as! PFFile
testObj["textField"] = self.textField.text
testObj["location"] = PFGeoPoint(latitude: currLocation!.latitude , longitude: currLocation!.longitude)
testObj["count"] = 0
testObj["replies"] = 0
testObj.saveInBackground()
self.dismissViewControllerAnimated(true, completion: nil)
}
else {
alert()
}
你什么都看不到的原因是你post把它弄错了class。根据图片BubbleTest
是名字class不是YikYakTest
替换此行
let testObj = PFObject(className: "YikYakTest")
来自
let testObj = PFObject(className: "BubbleTest")
您的代码应如下所示:
注意使用saveInBackgroundWithBlock方法,这样你可以检查保存时是否有错误
let testObj = PFObject(className: "BubbleTest")
let username = PFUser.currentUser()?.username
testObj["userName"] = username
testObj["textField"] = self.textField.text
testObj["Location"] = PFGeoPoint(latitude:currLocation.latitude , longitude: currLocation.longitude)
testObj["count"] = 0
testObj["replies"] = 0
testObj.saveInBackgroundWithBlock { (success:Bool, error :NSError?) -> Void in
if error == nil
{
print("detail is saved")
self.dismissViewControllerAnimated(true, completion: nil)
}
else
{
print("error")
}
}
当您保存 PFGeopoint 坐标时,将其保存到 Location
列而不是 location
我认识很多 运行 遇到类似问题的开发人员朋友。我自己也有这个问题,现在解决了。所以希望我可以从我从 Parse 查询数据中学到的知识中提供一些见解:
尝试将 numberOfSectionsInTableView 方法更改为 return 1 而不是 0像这样:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
您可能还需要一些数据结构来保存用户的帖子(消息):
var userPosts:NSMutableArray! = NSMutableArray()
此外,您的 table 视图的行数与您在 userPosts 中存储的帖子一样多:
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return userPosts.count
}
在cellForRowAtIndexPath中,替换为:
let object = PFObject(className: "BubbleTest")
有了这个:
let userPost : PFObject = self.posts.objectAtIndex(indexPath!.row) as! PFObject
...
cell.message.text = userPost.objectForKey("message") as! String
return cell
}
这会将自定义单元格消息的文本 属性 设置为用户消息的任何内容(即:"Testing 1 2")。
注意:这些步骤并不是解决您的问题所需的唯一步骤。它旨在通过一些基本步骤引导您朝着正确的方向前进。
希望对您有所帮助! :)
@IBAction func postPressed(sender: AnyObject) {
if(currLocation != nil){
let testObj = PFObject(className: "BubbleTest")
testObj["userName"] = PFUser.currentUser()?.username
//testObj["profileName"] = PFUser.valueForKey("profileName") as! String
//testObj["photo"] = PFUser.currentUser()?.valueForKey("photo") as! PFFile
testObj["textField"] = self.textField.text
testObj["location"] = PFGeoPoint(latitude: currLocation!.latitude , longitude: currLocation!.longitude)
testObj["count"] = 0
testObj["replies"] = 0
testObj.saveInBackground()
self.dismissViewControllerAnimated(true, completion: nil)
}
else {
alert()
}
你什么都看不到的原因是你post把它弄错了class。根据图片BubbleTest
是名字class不是YikYakTest
替换此行
let testObj = PFObject(className: "YikYakTest")
来自
let testObj = PFObject(className: "BubbleTest")
您的代码应如下所示:
注意使用saveInBackgroundWithBlock方法,这样你可以检查保存时是否有错误
let testObj = PFObject(className: "BubbleTest")
let username = PFUser.currentUser()?.username
testObj["userName"] = username
testObj["textField"] = self.textField.text
testObj["Location"] = PFGeoPoint(latitude:currLocation.latitude , longitude: currLocation.longitude)
testObj["count"] = 0
testObj["replies"] = 0
testObj.saveInBackgroundWithBlock { (success:Bool, error :NSError?) -> Void in
if error == nil
{
print("detail is saved")
self.dismissViewControllerAnimated(true, completion: nil)
}
else
{
print("error")
}
}
当您保存 PFGeopoint 坐标时,将其保存到 Location
列而不是 location
我认识很多 运行 遇到类似问题的开发人员朋友。我自己也有这个问题,现在解决了。所以希望我可以从我从 Parse 查询数据中学到的知识中提供一些见解:
尝试将 numberOfSectionsInTableView 方法更改为 return 1 而不是 0像这样:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
您可能还需要一些数据结构来保存用户的帖子(消息):
var userPosts:NSMutableArray! = NSMutableArray()
此外,您的 table 视图的行数与您在 userPosts 中存储的帖子一样多:
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return userPosts.count
}
在cellForRowAtIndexPath中,替换为:
let object = PFObject(className: "BubbleTest")
有了这个:
let userPost : PFObject = self.posts.objectAtIndex(indexPath!.row) as! PFObject
...
cell.message.text = userPost.objectForKey("message") as! String
return cell
}
这会将自定义单元格消息的文本 属性 设置为用户消息的任何内容(即:"Testing 1 2")。
注意:这些步骤并不是解决您的问题所需的唯一步骤。它旨在通过一些基本步骤引导您朝着正确的方向前进。
希望对您有所帮助! :)