无法在iOS Swift中正确加载Tableview数组?

时间:2019-07-05 10:53:37

标签: ios swift firebase uitableview

我正在使用聊天应用程序。将发送方和接收方消息保存到Firebase。尝试将数组消息加载到TableView时出现我的问题,消息没有完全加载。它显示剩余消息的间隔。

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

    }
}

}

这是我的屏幕截图:enter image description here

我收到的邮件数正确,但是邮件已满载。

任何帮助,不胜感激...

1 个答案:

答案 0 :(得分:1)

使用它。 heightForRowAt 获取单元格的高度,但您必须检查约束。 并使用 UITableViewDelegate 来使用它

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }