提问人:user16961399 提问时间:3/2/2023 最后编辑:user16961399 更新时间:3/2/2023 访问量:60
所有帖子都共享相同的评论,而不是每个帖子都有独特的评论 iOS 社交媒体应用程序
Posts all share the same comments instead of each one having unique comments iOS Social Media App
问:
我目前正在开发 Xcode 14.2 上的社交媒体应用程序。在这一点上,我正在集成一个功能性评论系统,用户可以在其中查看帖子的现有评论并留下自己的评论。我已经让它在某种程度上起作用了,因为一个问题困扰着我。
它目前的工作原理是,在每个帖子下都会有一个用作按钮的文本符号。当用户按下按钮时,他们会被重定向到评论部分,在那里他们可以查看其他用户所说的话,也可以通过按创建评论按钮来制作自己的评论。问题在于,当用户发表评论时,评论会显示在应用程序上所有现有帖子的下方,而不仅仅是用户选择评论的帖子。我如何确保每个帖子都有自己独特的评论部分,以便当用户发表评论时,它只显示在该帖子上,而不是在所有帖子中显示评论?
以下是涉及的代码:
CommentViewController
import UIKit
import Parse
class CommentViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
var selectedPost: PFObject!
var comments: [PFObject] = []
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
loadComments()
}
func loadComments()
{
let query = PFQuery(className: "Comment")
query.whereKey("post", equalTo: selectedPost)
query.includeKey("author")
query.findObjectsInBackground
{
[weak self] comments, error in
if let comments = comments
{
self?.comments = comments
self?.tableView.reloadData()
} else if let error = error
{
print("Error loading comments: \(error.localizedDescription)")
}
}
}
@IBAction func onCreateComment(_ sender: Any)
{
let alertController = UIAlertController(title: "Add Comment", message: nil, preferredStyle: .alert)
alertController.addTextField
{
textField in
textField.placeholder = "Enter your comment here"
}
let postAction = UIAlertAction(title: "Post", style: .default)
{
[weak self] _ in
guard let commenttext = alertController.textFields?.first?.text
else
{
return
}
let newComment = PFObject(className: "Comment")
newComment["author"] = PFUser.current() ?? NSNull()
newComment["text"] = commenttext
newComment["post"] = self?.selectedPost ?? NSNull()
newComment.saveInBackground
{
[weak self] success, error in
if success
{
self?.loadComments()
}
else if let error = error
{
print("Error saving comment: \(error.localizedDescription)")
}
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(postAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return comments.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentCell
let comment = comments[indexPath.row]
cell.commentTextLabel.text = comment["text"] as? String
if let user = comment["author"] as? PFUser
{
cell.nameLabel.text = user.username
}
else
{
cell.nameLabel.text = "Unknown"
}
return cell
}
}
CommentCell
import UIKit
class CommentCell: UITableViewCell {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var commentTextLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
任何和所有的反馈/建议将不胜感激!
更新:我已更改为 .CommentViewController 的工作方式是,有一个按钮连接到它,按下时以模式显示。以下是我现在的样子。selectedPost
postObject
FeedViewController
CommentViewController
import UIKit
import Parse
class CommentViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
var postObject: PFObject!
var comments: [PFObject] = []
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
if postObject != nil
{
loadComments()
}
}
func loadComments()
{
guard let postObject = postObject else
{
print("Error: postObject is nil")
return
}
guard let postId = postObject.objectId else
{
print("Error: postId is nil")
return
}
let postQuery = PFQuery(className: "Posts")
postQuery.whereKey("objectId", equalTo: postId)
let query = PFQuery(className: "Comment")
query.whereKey("post", equalTo: postObject)
query.whereKey("post", matchesQuery: postQuery)
query.includeKey("author")
query.findObjectsInBackground
{
[weak self] comments, error in
if let comments = comments
{
self?.comments = comments
self?.tableView.reloadData()
} else if let error = error
{
print("Error loading comments: \(error.localizedDescription)")
}
}
}
@IBAction func onCreateComment(_ sender: Any)
{
let alertController = UIAlertController(title: "Add Comment", message: nil, preferredStyle: .alert)
alertController.addTextField
{
textField in
textField.placeholder = "Enter your comment here"
}
let postAction = UIAlertAction(title: "Post", style: .default)
{
[weak self] _ in
guard let commenttext = alertController.textFields?.first?.text
else
{
return
}
let newComment = PFObject(className: "Comment")
newComment["author"] = PFUser.current() ?? NSNull()
newComment["text"] = commenttext
newComment["post"] = self?.postObject ?? NSNull()
newComment.saveInBackground
{
[weak self] success, error in
if success
{
self?.loadComments()
}
else if let error = error
{
print("Error saving comment: \(error.localizedDescription)")
}
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(postAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return comments.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentCell
let comment = comments[indexPath.row]
cell.commentTextLabel.text = comment["text"] as? String
if let user = comment["author"] as? PFUser
{
cell.nameLabel.text = user.username
}
else
{
cell.nameLabel.text = "Unknown"
}
return cell
}
}
可悲的是,我仍然收到错误postObject is nil。
更新 3:以下是一些涉及和FeedViewController
postObject
CommentViewController
var postObject: PFObject?
//...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let commentVC = storyboard?.instantiateViewController(withIdentifier: "CommentViewController") as! CommentViewController
if let postObject = postObject
{
commentVC.postObject = postObject
}
present(commentVC, animated: true, completion: nil)
}
```
答:
问题似乎来自你的方法。假设 in 中的每一行代表一个帖子,您需要获取所选行的帖子对象。didSelectRowAt
FeedViewController
FeedViewController
我假设在一个名为(根据您实际使用的任何内容进行调整)的属性中有一个帖子数组。鉴于此,您的方法需要如下所示:FeedViewController
posts
didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let commentVC = storyboard?.instantiateViewController(withIdentifier: "CommentViewController") as! CommentViewController
commentVC.postObject = posts[indexPath.row]
present(commentVC, animated: true, completion: nil)
}
换句话说,获取所选行的 post 对象,就像您在方法中所做的那样。cellForRowAt
评论
postObject
下一个:不符合协议
评论
selectedPost
loadComments()
selectedPost