gpt4 book ai didi

ios - Tableview 数组未在 iOS Swift 中正确加载?

转载 作者:行者123 更新时间:2023-11-28 13:34:09 24 4
gpt4 key购买 nike

我在聊天应用程序中工作。将发送者和接收者消息保存到 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)
}
}
}

ma​​inChatScreenTableViewCell

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

我得到正确的消息计数,但消息已完全加载。

非常感谢任何帮助...

最佳答案

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

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

关于ios - Tableview 数组未在 iOS Swift 中正确加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56901673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com