I’m at present engaged on a social media app on Xcode 14.2. At this level, I’m as much as integrating a useful feedback system, the place customers can view present feedback for a submit and depart their very own. I’ve gotten it to work considerably, as a problem is plaguing me.
The way it at present works is that below every submit there could be a textual content image that capabilities as a button. When the consumer presses the button, they get redirected to the feedback part, the place they’ll view what different customers say in addition to make their very own by urgent the create remark button. The problem is that when a consumer leaves a remark, the remark reveals up beneath the entire present posts on the app, slightly than simply the submit the consumer selected to touch upon. How do I am going about ensuring every submit has its personal distinctive feedback part such that when a consumer leaves a remark it solely reveals on that submit, slightly than having the remark present throughout all posts?
Right here is the code concerned:CommentViewController
import UIKit
import Parse
class CommentViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
var selectedPost: PFObject!
var feedback: [PFObject] = []
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
tremendous.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
loadComments()
}
func loadComments()
{
let question = PFQuery(className: "Remark")
question.whereKey("submit", equalTo: selectedPost)
question.includeKey("creator")
question.findObjectsInBackground
{
[weak self] feedback, error in
if let feedback = feedback
{
self?.feedback = feedback
self?.tableView.reloadData()
} else if let error = error
{
print("Error loading feedback: (error.localizedDescription)")
}
}
}
@IBAction func onCreateComment(_ sender: Any)
{
let alertController = UIAlertController(title: "Add Remark", message: nil, preferredStyle: .alert)
alertController.addTextField
{
textField in
textField.placeholder = "Enter your remark right here"
}
let postAction = UIAlertAction(title: "Submit", type: .default)
{
[weak self] _ in
guard let commenttext = alertController.textFields?.first?.textual content
else
{
return
}
let newComment = PFObject(className: "Remark")
newComment["author"] = PFUser.present() ?? 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 remark: (error.localizedDescription)")
}
}
}
let cancelAction = UIAlertAction(title: "Cancel", type: .cancel, handler: nil)
alertController.addAction(postAction)
alertController.addAction(cancelAction)
current(alertController, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
return feedback.rely
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentCell
let remark = feedback[indexPath.row]
cell.commentTextLabel.textual content = remark["text"] as? String
if let consumer = remark["author"] as? PFUser
{
cell.nameLabel.textual content = consumer.username
}
else
{
cell.nameLabel.textual content = "Unknown"
}
return cell
}
}
CommentCell
import UIKit
class CommentCell: UITableViewCell {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var commentTextLabel: UILabel!
override func awakeFromNib() {
tremendous.awakeFromNib()
// Initialization code
}
override func setSelected(_ chosen: Bool, animated: Bool) {
tremendous.setSelected(chosen, animated: animated)
// Configure the view for the chosen state
}
}
Any and all suggestions/recommendations might be significantly appreciated!
Replace: I’ve modified selectedPost
to postObject
. The way in which CommentViewController works by way of an occasion of it being made is there is a button in FeedViewController
related to it that presents modally when pressed. The next is how my CommentViewController
appears now.
import UIKit
import Parse
class CommentViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
var postObject: PFObject!
var feedback: [PFObject] = []
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
tremendous.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 question = PFQuery(className: "Remark")
question.whereKey("submit", equalTo: postObject)
question.whereKey("submit", matchesQuery: postQuery)
question.includeKey("creator")
question.findObjectsInBackground
{
[weak self] feedback, error in
if let feedback = feedback
{
self?.feedback = feedback
self?.tableView.reloadData()
} else if let error = error
{
print("Error loading feedback: (error.localizedDescription)")
}
}
}
@IBAction func onCreateComment(_ sender: Any)
{
let alertController = UIAlertController(title: "Add Remark", message: nil, preferredStyle: .alert)
alertController.addTextField
{
textField in
textField.placeholder = "Enter your remark right here"
}
let postAction = UIAlertAction(title: "Submit", type: .default)
{
[weak self] _ in
guard let commenttext = alertController.textFields?.first?.textual content
else
{
return
}
let newComment = PFObject(className: "Remark")
newComment["author"] = PFUser.present() ?? 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 remark: (error.localizedDescription)")
}
}
}
let cancelAction = UIAlertAction(title: "Cancel", type: .cancel, handler: nil)
alertController.addAction(postAction)
alertController.addAction(cancelAction)
current(alertController, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
return feedback.rely
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentCell
let remark = feedback[indexPath.row]
cell.commentTextLabel.textual content = remark["text"] as? String
if let consumer = remark["author"] as? PFUser
{
cell.nameLabel.textual content = consumer.username
}
else
{
cell.nameLabel.textual content = "Unknown"
}
return cell
}
}
Sadly I nonetheless get the error postObject is nil.
Replace 3: Right here is a few code from FeedViewController
that entails postObject
and 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
}
current(commentVC, animated: true, completion: nil)
}
```