Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在JSQMessagesViewController中显示消息日期_Ios_Swift_Date_Chat_Jsqmessagesviewcontroller - Fatal编程技术网

Ios 如何在JSQMessagesViewController中显示消息日期

Ios 如何在JSQMessagesViewController中显示消息日期,ios,swift,date,chat,jsqmessagesviewcontroller,Ios,Swift,Date,Chat,Jsqmessagesviewcontroller,我正在尝试制作一个聊天应用程序,我正在使用JSQMessagesViewController库。 我的聊天视图显示所有带有日期和时间的消息,但是我在这个库中遇到了一些小问题。 我的问题是我只想显示一次消息日期。 假设: 昨天日期包含10条消息 今天的日期包含15条消息 我只想显示这些消息的单个日期,而不是每个消息的重复日期。使用此方法执行此操作 func collectionView(collectionView: JSQMessagesCollectionView, attributedTex

我正在尝试制作一个聊天应用程序,我正在使用JSQMessagesViewController库。 我的聊天视图显示所有带有日期和时间的消息,但是我在这个库中遇到了一些小问题。 我的问题是我只想显示一次消息日期。 假设: 昨天日期包含10条消息 今天的日期包含15条消息


我只想显示这些消息的单个日期,而不是每个消息的重复日期。使用此方法执行此操作

func collectionView(collectionView: JSQMessagesCollectionView, attributedTextForCellTopLabelAtIndexPath indexPath: NSIndexPath) -> NSAttributedString? { 

如果日期与上一条消息的返回日期不同,则必须检查消息的日期,否则返回的格式为零。

您需要确定消息是否是当天的第一条消息。然后在新的一天之前,为所有其他人返回零

我创建了一个函数来确定我的消息是否是那个人第一次发送的。看起来像这样

func firstMessageOfTheDay(indexOfMessage: NSIndexPath) -> Bool { 
  let messageDate = messages[indexOfMessage.item].date
  guard let previouseMessageDate = messages[indexOfMessage.item - 1].date else {
    return true // because there is no previous message so we need to show the date
  }
    let day = Calendar.current.component(.day, from: messageDate)
    let previouseDay = Calendar.current.component(.day, from: previouseMessageDate)
    if day == previouseDay {
        return false
    } else {
        return true
    }
}

然后在适当的函数中调用它。如果您有任何其他问题,希望这有助于让我知道。

谢谢Daniel这是我需要的非常完美的答案我很高兴它帮助了Hi Umesh,您能发布显示日期的全部代码吗?我找不到这样做的方法you@DilipTiwari我能为您提供什么帮助?您应该检查传入消息的顺序。这是我的代码bro bro您需要了解异步操作。在第153行,您正在下载图像,然后将其添加到消息列表中。让我们来看看。