ios - collectionView 中每个单元格的时间戳

标签 ios swift uicollectionview uicollectionviewcell

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

enter image description here

这是 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类型(核心数据),它有3个属性:usserMessage, isFromApi, and 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
    }
}

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

最佳答案

您在运行时获取日期,因此对于每个单元格,它只会显示当前时间,在您的例子中是 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
    }
}

关于ios - collectionView 中每个单元格的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49485622/

相关文章:

ios - asyncAfter 不延迟指定时间的第一次执行

ios - Facebook 个人资料的 iOS URL 架构是什么?

swift - 从 CollectionViewCell 重新加载 TableView

ios - 指定要在 iOS 上的 audiounit 渲染回调中处理的帧数

ios - 如何处理 phonegap cordova 上的 iOS 通知回调?

ios - 运行此代码时如何以及为何会出现信号 SIGABRT?

ios - UICollectionView reloadData 但 View 闪烁

ios - collectionView 函数 didSelectItemAt indexPath 将数据传递到下一个 View Controller

ios - Swift 模块稳定性 : Module compiled with Swift X. Y 无法被 Swift X.Z 编译器导入

ios - 用单位标记核心图轴