collectionView中每个单元格的时间戳

时间:2018-03-26 06:59:52

标签: 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文件中的configure函数 ,消息:类型为NSManagedOBject(核心数据),它有3个属性:usserMessageisFromApitimeDate

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
    }
}

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

2 个答案:

答案 0 :(得分:2)

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

let currentDateTime = Date()

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

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


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

答案 1 :(得分:2)

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

请尝试以下方法:

  

在模式中添加时间属性Messages.swift

var timeString: String?
  

collectionViewCell.swift中,按以下步骤更新configure()功能:

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

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

    } else {
        // time is not present in modal, 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 modal, 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
    }
}