0

I'm working in a chat application. Saving sender and receiver messages to firebase. My issue when tried to load array message to TableView, messages are not completely loaded. it shows gap for remaining messages.

MainChatViewController.swift

var messages = [Message]()

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return messages.count   
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    print("message count::\(messages.count)")
    
    let message = messages[indexPath.row]
    
    if let cell = chatTableView.dequeueReusableCell(withIdentifier: "Message") as? mainChatScreenTableViewCell {
        cell.tag = indexPath.row
        cell.configCell(message: message)
        return cell
    } else {
        return mainChatScreenTableViewCell()
    }
}

func loadData() {

 Database.database().reference().child("messages").child(messageId)
.observe(.value, with: { (snapshot) in
        
        if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
            
            self.messages.removeAll()
            
            for data in snapshot {
                
                if let postDict = data.value as? Dictionary<String, AnyObject> 
  {
                    
                    let key = data.key
                    
                    let post = Message(messageKey: key, postData: postDict)
                    
                    self.messages.append(post)
                }
            }
        }
        
        self.chatTableView.reloadData()
    })
    
}//loadData

func moveToBottom() {
    DispatchQueue.main.async {
        if self.messages.count > 0  {
            let indexPath = IndexPath(row: self.messages.count - 1, section: 0)
            self.chatTableView.scrollToRow(at: indexPath, at: .bottom, animated: false)
        }
    }
}

mainChatScreenTableViewCell

import UIKit
import SwiftKeychainWrapper
import Firebase
import FirebaseStorage
import FirebaseDatabase

class mainChatScreenTableViewCell: UITableViewCell {


@IBOutlet weak var recievedMessageLbl: UILabel!

@IBOutlet weak var recievedMessageView: UIView!

@IBOutlet weak var sentMessageLbl: UILabel!

@IBOutlet weak var sentMessageView: UIView!

@IBOutlet var receivedTimeLabel: UILabel!

@IBOutlet var sentTimeLabel: UILabel!

@IBOutlet var likeOrUnlikeImageView: UIImageView!

@IBOutlet var errorImageView: UIImageView!


@IBOutlet var checkImage: UIImageView!

var message: Message!
var currentUser = KeychainWrapper.standard.string(forKey: "uid")

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    
    sentMessageView.layer.masksToBounds  = true
    recievedMessageView.layer.masksToBounds = true
   
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

func configCell(message: Message) {
    
    self.message = message
    
    print("message label::\(message.message.count)")
    if message.sender == currentUser {
        
        let time = message.receivedTimeStamp
        let timeinterval : TimeInterval = time
        let dateFromServer = NSDate(timeIntervalSince1970:timeinterval)
        let formatter = DateFormatter()
        formatter.calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.ISO8601) as Calendar?
        formatter.locale = NSLocale(localeIdentifier: "en_IN") as Locale
        formatter.timeZone = NSTimeZone(name: "GMT+5:30") as TimeZone?
        
        formatter.dateFormat = "h:mm a"
        formatter.amSymbol = "AM"
        formatter.pmSymbol = "PM"
        let dateString: String = formatter.string(from: dateFromServer as Date)
        
        print("dateString:::\(dateString)")
        
        sentMessageView.isHidden = false
        
        sentMessageView.layer.backgroundColor = UIColor.clear.cgColor
        
        sentMessageLbl.text = " " + message.message
        sentTimeLabel.text = " " + dateString
        
        recievedMessageLbl.text = ""
        
        recievedMessageLbl.isHidden = true
        
        recievedMessageView.isHidden = true
        
    } else {

        let time = message.receivedTimeStamp
        let timeinterval : TimeInterval = time
        let dateFromServer = NSDate(timeIntervalSince1970:timeinterval)
        let formatter = DateFormatter()
        formatter.calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.ISO8601) as Calendar?
        formatter.locale = NSLocale(localeIdentifier: "en_IN") as Locale
        formatter.timeZone = NSTimeZone(name: "GMT+5:30") as TimeZone?

        formatter.dateFormat = "h:mm a"
        formatter.amSymbol = "AM"
        formatter.pmSymbol = "PM"
        let dateString: String = formatter.string(from: dateFromServer as Date)

        print("dateString:::\(dateString)")
        
        sentMessageView.isHidden = true
        sentMessageLbl.isHidden = true
        sentMessageLbl.text = ""
        
        recievedMessageLbl.text = " " + message.message

        receivedTimeLabel.text = " " + dateString
        
        recievedMessageLbl.isHidden = false
        
        recievedMessageView.layer.backgroundColor = UIColor.clear.cgColor

    }
}

}

Here is my screenshot: enter image description here

I'm getting correct message count but messages are fully loaded.

Any help much appreciated pls...

7
  • Can you post the tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { method implementation? Commented Jul 5, 2019 at 11:11
  • @claudio i have added numberOfRowsInSection. check it out Commented Jul 5, 2019 at 11:22
  • What about mainChatScreenTableViewCell() method ? is this for any header or something else? Commented Jul 5, 2019 at 11:47
  • Inside mainChatScreenTableViewCell() i have used configCell(message: Message) function @CodeChanger Commented Jul 5, 2019 at 11:52
  • please add code for mainChatScreenTableViewCell() Commented Jul 5, 2019 at 12:18

1 Answer 1

1

use it. heightForRowAt for height of cell but you must check your constraint. and also use UITableViewDelegate to use it

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
Sign up to request clarification or add additional context in comments.

2 Comments

i think i need to check my constraints... Thanks for answering
@PvDev welcome. please add my answer as a favourite answer if it is use full for you. that can helps for others

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.