title: Swift tableView Cells disappears on scroll
body: <p>Trying to add a chat function in my app with tableView but whenever i scroll my chats some cells disappear and i am not sure why.  Any help is appreciated.</p>
  
  <p>The tableView is connected to dataSource and delegate in the storyboard.</p>
  
  <p>I have created the cell itself in a xib file and depending on if it is a sender message or received message the cell will display different.
  This is the code for each cell:</p>
  
  <pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "customMessageCell", for: indexPath) as! CustomMessageCell
  
      //Sets cell to message
  
      senderId = messageArray[indexPath.row].fromId
  
      if senderId == Auth.auth().currentUser?.uid as String? {
          //Messages We Sent 
          cell.ourMessageBody.text = messageArray[indexPath.row].messageBody
  
          cell.ourMessageBackground.backgroundColor = UIColor(displayP3Red: 59/255.0, green: 89/255.0, blue: 152/255.0, alpha: 1)
          cell.ourMessageBody.textColor = UIColor.white
  
          cell.avatarImageView.isHidden = true
          cell.messageBackground.isHidden = true
  
      } else{
          //Messages someone else sent
          cell.messageBody.text = messageArray[indexPath.row].messageBody
          cell.avatarImageView.backgroundColor = UIColor(white: 0.95, alpha: 1)
          cell.messageBackground.backgroundColor = UIColor(white: 0.95, alpha: 1)
          cell.ourMessageBackground.isHidden = true
          //toId ProfileImage
          if let imageID = toId{
              print(imageID)
              let imagesStorageRef = Storage.storage().reference().child("profilepic/").child(imageID)
              imagesStorageRef.getData(maxSize: 1*1024*1024, completion: { (data, error) in
                  if error != nil{
                      print(error)
                      return
                  }
                  DispatchQueue.main.async {
                      cell.avatarImageView?.image = UIImage(data: data!)
                  }
  
              })
  
          }
      }
  
      return cell
  }
  </code></pre>
  
  <p>And here is the method for retrieving messages:</p>
  
  <pre><code>    func retrieveMessages() {
  
      let testUserOrAd = SwipingView.myAdvertiserVar.advertiser
      print("testing result: \(testUserOrAd) your are a User")
  
      //if advertiser
      if testUserOrAd == true{
          if let testId = self.toId{
              let MessageDB = Database.database().reference().child("Users").child(toId!).child("Messages").child(uid!)
  
              MessageDB.observe(.childAdded) { (snapshot) in
                  let snapshotValue =  snapshot.value as! Dictionary&lt;String,String&gt;
  
                  let text = snapshotValue["MessageBody"]!
                  let fromId = snapshotValue["FromId"]
  
                  let message = Message()
                  message.messageBody = text
                  message.fromId = fromId
  
                  self.messageArray.append(message)
  
                  self.configureTableView()
                  self.messageTableView.reloadData()
              }
          }
  
      }else{
          //if user
  
          let MessageDB = Database.database().reference().child("Users").child(uid!).child("Messages").child(toId!)
  
          MessageDB.observe(.childAdded) { (snapshot) in
              let snapshotValue =  snapshot.value as? [String: AnyObject]
  
              let text = snapshotValue!["MessageBody"] as? String
              let fromId = snapshotValue!["FromId"] as? String
  
              let message = Message()
              message.messageBody = text
              message.fromId = fromId
  
              self.messageArray.append(message)
  
              self.configureTableView()
              self.messageTableView.reloadData()
          }
  
      }
  }
  </code></pre>
  
  <p>And this is the result:</p>
  
  <p><a href="https://i.stack.imgur.com/Bsk3c.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Bsk3c.gif" alt="enter image description here"></a></p>