collectionView 中每个单元格的时间戳

Timestamp for each cell in collectionView

我有一个 collectionView 聊天,我想显示每条消息的时间。

这里是 dequeueReusable 单元格函数:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath) as! ChatMessageCollectionVIewCell
    let message = messagesArray[indexPath.row]

    cell.configure(with: message)



    return cell

}

这是我的 collectionViewCell 文件中的配置函数 , Messages: 是NSManagedOBject(core data)类型,有3个属性:usserMessage, isFromApi, timeDate.

func configure(with message:Messages)
{
    messageOutlet.text = message.userMessage
    viewForMessages.backgroundColor = UIColor.lightGray


    let currentDateTime = Date()
    let formatter = DateFormatter()
    formatter.timeStyle = .short
    formatter.dateStyle = .none


    timeLabel.text = formatter.string(from: currentDateTime)


    if message.isFromApi == true
    {
        viewForMessages.backgroundColor = .lightGray
        trailingConstraint.constant = 40
        leadingConstraint.constant = 10
    }
    else
    {
        viewForMessages.backgroundColor = #colorLiteral(red: 0, green: 0.5898008943, blue: 1, alpha: 1)
        trailingConstraint.constant = 10
        leadingConstraint.constant = 40
    }
}

目前它正确地显示了第一条消息的时间,但是当我在另一分钟添加另一条消息时,它也会更改第一条消息的时间。

每次渲染单元格时都会调用

let currentDateTime = Date()

您需要在创建消息对象时将时间存储在消息对象中,并在每次调用 configure 方法时读取它的值。类似于:

let messageDate = message.messageDate
let formatter = DateFormatter()
formatter.timeStyle = .short
formatter.dateStyle = .none


timeLabel.text = formatter.string(from: messageDate)

您在运行时获取日期,因此对于每个单元格,它只会显示当前时间,在您的情况下说 9:58。为避免这种情况,您需要将时间存储在消息模型中。

尝试以下操作:

Add time property in your model, Messages.swift

var timeString: String?

In collectionViewCell.swift, Update your configure() function as following:

func configure(with message: Messages) {
    messageOutlet.text = message.userMessage
    viewForMessages.backgroundColor = UIColor.lightGray

    // Check whether time is already present in model or you need to set it as current time
    if let time = message.timeString {
        // time is already present in model
        timeLabel.text = time

    } else {
        // time is not present in model, you need to set it with current time
        let currentDateTime = Date()
        let formatter = DateFormatter()
        formatter.timeStyle = .short
        formatter.dateStyle = .none

        let formattedTime = formatter.string(from: currentDateTime)
        message.timeString = formattedTime // Save the time in model, for future use
        timeLabel.text = formattedTime
    }

    if message.isFromApi == true {
        viewForMessages.backgroundColor = .lightGray
        trailingConstraint.constant = 40
        leadingConstraint.constant = 10
    } else {
        viewForMessages.backgroundColor = #colorLiteral(red: 0, green: 0.5898008943, blue: 1, alpha: 1)
        trailingConstraint.constant = 10
        leadingConstraint.constant = 40
    }
}