UITableView - Design for comments and replies

Hello everyone,

I am currently working on having an application to display comments with respective replies.

I can show all the comments easily in a table view having a simple array of comment objects. In case, one of the comment has any replies (in the database stored), I need to load those replies - which are represented as a different objects at the moment - and display them under the correct comment.

For the table view, this means, new rows are going to be inserted.

I tried to code this but somehow, this ends up in an endless loop. Maybe you guys can give me a hint?


This is my function to load the comments first
Code Block

Code Block
func fetchCommentsByParticipationId() {
    dataAccessService.fetchCommentsByParticipationId(completionHandler: { (commentArray) in
      self.commentArray = commentArray
      self.commentTableView.reloadData()
    }, participationId: participationObject.id)
  }

I have two arrays as data sources. One contains the comments and the other ones contains the comment replies
Code Block
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return commentArray.count + commentReplyArray.count
  }

Code Block
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let commentsTableViewCell = tableView.dequeueReusableCell(withIdentifier: "CommentsTableViewCell") as! CommentsTableViewCell
    commentsTableViewCell.backgroundColor = DARK_BLUE_COLOR
     
    let cellHeight = commentsTableViewCell.frame.height
    let cellWidth = commentsTableViewCell.frame.width
     
    let commentObject = commentArray[indexPath.row] // Comment(json: (hitsTableController.hitsSource?.hit(atIndex: indexPath.row))!)
     
    commentsTableViewCell.initSenderLabel(cellWidth: cellWidth, cellHeight: cellHeight)
    commentsTableViewCell.initReplyButton(cellWidth: cellWidth, cellHeight: cellHeight)
    dataAccessService.fetchUserById(completionHandler: { (userObject) in
      commentsTableViewCell.senderLabel.text = userObject.firstName + " " + userObject.lastName
    }, uid: commentObject.userId)
     
    commentsTableViewCell.initCommentLabel(cellWidth: cellWidth, cellHeight: cellHeight)
    commentsTableViewCell.commentTextView.text = commentObject.comment
     
    commentsTableViewCell.replyButtonAction = { [unowned self] in
      self.commentReplyObject.commentId = commentObject.id
      self.commentReplyObject.userId = commentObject.userId
      var replyToUserName = commentsTableViewCell.senderLabel.text!
      replyToUserName = replyToUserName.replacingOccurrences(of: " ", with: "")
       
      self.replyToUser(replyToUserName: replyToUserName)
    }
     
    // fetch replies to comments
    dataAccessService.fetchRepliesByCommentId(completionHandler: { (_commentReplyArray) in
      for commentReply in _commentReplyArray {
        self.commentReplyArray.append(commentReply)
        self.commentTableView.insertRows(at: [
          (NSIndexPath(row: self.commentArray.count-1, section: 0) as IndexPath)], with: .bottom)
        commentsTableViewCell.initReplyViews(cellWidth: cellWidth, cellHeight: cellHeight)
         
        commentsTableViewCell.senderLabel.text = "Test"
        commentsTableViewCell.commentTextView.text = commentReply.comment
      }
    }, commentId: commentObject.id)
    return commentsTableViewCell
  }

```

What would be the best idea to achieve what I want to do?
You are doing things in cellForRowAt that should not be there.

Here you should only use datasources to build the cells, not do the fetching. This has to be done in IBAction.
UITableView - Design for comments and replies
 
 
Q