2021年4月9日星期五

Comment Cell isn’t automatically resizing after a comment is posted

I am having trouble getting my comment cells to resize properly after a comment is posted.

When the Comment View first loads, everything looks good.

However, right after a comment is posted, the cells still retain their previous size and do not automatically resize.

This leads to new comment cells having the wrong dimensions (see pictures):

enter image description here

  1. The comment view when it first loads. Comment cells are the correct size.

  2. About to post a one line comment.

  3. Oh no: The new comment is posted but the old cell sizes are used instead.


The AutoLayout constraints for the Comment Cell are done in the storyboard.

The numberOfLines attribute for the comment text view is set to 0.


In the CommentViewController, estimatedRowHeight and automaticDimension are used:

    self.tableView.estimatedRowHeight = 77                    self.tableView.rowHeight = UITableView.automaticDimension  

Edit: layoutIfNeeded() is used as well.

In the CommentTableViewCell class, prepareForReuse is called:

    override func prepareForReuse()      {          super.prepareForReuse()      }  

After the new comment is posted, pulling to refresh does not readjust the cell size. The only way to fix it is to click the back button and re-enter the Comment View.

How do I fix this problem?

Thank you for your time.


Edit: Added screenshot of my storyboard, heirarchy and constraints

enter image description here


EDIT: CODE FOR POSTING COMMENTS

This is what happens when a comment is posted:

1. The send button is pressed

    // Actions performed when the Send button is pressed      @IBAction func sendButton_TouchUpInside(_ sender: UIButton)      {          // Store the commenter's user ID          guard let commenterUid = Api.User.CURRENT_USER?.uid else          {              return          }                    // Store the user id of the user that made the post          let posterUserId = post.uid!                    // Store the comment text          let commentText = self.commentTextField.text!                    // Post the comment          self.postComment()      }  

2. The postComment() function is called

    // This function posts a comment      func postComment()      {          // Store the commenter's user id          let commenterUid = Api.User.CURRENT_USER!.uid                    // Store the comment text          let commentText = self.commentTextField.text!                    // Remove all text from the comment text field          self.empty()                    // Hide keyboard          self.view.endEditing(true)                    // Add the comment to the database          Api.Comment.addComment(postId: self.postId, commenterUid: commenterUid, commentText: commentText, completion: {                // Refresh the comment feed              self.refresh()          })      }  

3. The view is refreshed using refresh()

    // This method refreshes comments in the comment feed      @objc func refresh()      {             // Remove all old comments in the comments array          self.comments.removeAll()                // Remove all old users in the user array          self.users.removeAll()                // Retrieve comments from the database          loadComments()      }  

4. Comments are re-loaded in the table view using loadComments()

    // This method loads the comments for a post      func loadComments()      {          // The app has started to load comments          self.isLoadingComment = true                   // Query comments for the comment view          // The limit parameter is the number of comments that should be loaded at once          Api.Post_Comment.getRecentComments(withPostId: postId, start: comments.first?.timestamp, limit: 10, completion:  {                            results in                            // If results is not an empty array,              if results.count > 0              {                  // Iterate through the results array and                  results.forEach({ (result) in                                            // Append the result's comment to the comments array                      self.comments.append(result.0)                                            // Append the result's user to the users array                      self.users.append(result.1)                  })              }                            // The app is no longer loading comments              self.isLoadingComment = false                            // If the refresh controller is refreshing,              if self.refreshControl.isRefreshing              {                  // End the refreshing operation                  self.refreshControl.endRefreshing()              }                            // Reload the table view data              self.tableView.reloadData()                            // Unhide the images, views, text fields and buttons              self.unhideViewElements()          })      }  

After the comments are reloaded, the comment cell constraints fail to adjust to the comments properly (see first picture). How do I fix this?

Thank you for your time.

https://stackoverflow.com/questions/67029213/comment-cell-isn-t-automatically-resizing-after-a-comment-is-posted April 10, 2021 at 06:55AM

1 条评论:

  1. Investing from the bitcoin market is extremely insecure and extremely profitable. Where there is a great deal of possibility there is a great deal of gain. Any brand new investor needs to take this into account as he can discard all of his or her assets. Bitcoin is quite profitable if you understand just how exactly to get it done. Thanks to the bitcoin market and trading, even a great deal of fresh millionaires have been developed.

    回复删除