它可能改变collectionView的逻辑:attributedTextForCellTopLabelAtIndexPath:显示日期时间戳的委托方法不是由indexPath.item%4 == 0? SOMessaging一天一天怎么样?或者其他什么?
此编码用于显示时间戳。
- (CGFloat)collectionView:(JSQMessagesCollectionView *)collectionView
layout:(JSQMessagesCollectionViewFlowLayout *)collectionViewLayout heightForCellTopLabelAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.item % 3 == 0) {
return kJSQMessagesCollectionViewCellLabelHeightDefault;
}
return 0.0f;
}
当前现有逻辑显示相同的时间戳重复如下。
答案 0 :(得分:22)
因为每个JSQMessage
对象都有date
属性,所以您只需将每条消息的日期与上一条消息的日期进行比较即可。
[thisMessageDate timeIntervalSinceDate:(NSDate *)previousMessageDate]
会在几秒钟内给你带来差异。如果差异大于,例如,一分钟(或您想要的任何时间间隔),则显示时间戳。
我正是这样做的:
- (NSAttributedString *)collectionView:(JSQMessagesCollectionView *)collectionView attributedTextForCellTopLabelAtIndexPath:(NSIndexPath *)indexPath {
JSQMessage *message = [self.messages objectAtIndex:indexPath.item];
if (indexPath.item == 0) {
return [[JSQMessagesTimestampFormatter sharedFormatter] attributedTimestampForDate:message.date];
}
if (indexPath.item - 1 > 0) {
JSQMessage *previousMessage = [self.messages objectAtIndex:indexPath.item - 1];
if ([message.date timeIntervalSinceDate:previousMessage.date] / 60 > 1) {
return [[JSQMessagesTimestampFormatter sharedFormatter] attributedTimestampForDate:message.date];
}
}
return nil;
}
然后重复此逻辑以确保时间戳具有正确的高度:
- (CGFloat)collectionView:(JSQMessagesCollectionView *)collectionView
layout:(JSQMessagesCollectionViewFlowLayout *)collectionViewLayout heightForCellTopLabelAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.item == 0) {
return kJSQMessagesCollectionViewCellLabelHeightDefault;
}
if (indexPath.item - 1 > 0) {
JSQMessage *previousMessage = [self.messages objectAtIndex:indexPath.item - 1];
JSQMessage *message = [self.messages objectAtIndex:indexPath.item];
if ([message.date timeIntervalSinceDate:previousMessage.date] / 60 > 1) {
return kJSQMessagesCollectionViewCellLabelHeightDefault;
}
}
return 0.0f;
}
答案 1 :(得分:1)
仅显示时间戳单元格day by day
;
根据@cerenali
的回答,我们可能会遇到有不同日期但接近时间的日期问题。像:
msgDate1 = 31/03/2016 23:55
msgDate2 = 01/04/2016 00:07
为了解决这个问题,我用if
替换了BOOL checkTime = message.date.year != previousMessage.date.year || message.date.month != previousMessage.date.month || message.date.day != previousMessage.date.day;
中的逻辑:
JSQMessage *message = [self.messages objectAtIndex:indexPath.item];
if (indexPath.item == 0) {
return [[JSQMessagesTimestampFormatter sharedFormatter] attributedTimestampForDate:message.date];
}
if (indexPath.item - 1 > -1) {
JSQMessage *previousMessage = [self.messages objectAtIndex:indexPath.item - 1];
BOOL checkTime = message.date.year != previousMessage.date.year || message.date.month != previousMessage.date.month || message.date.day != previousMessage.date.day;
if (checkTime) {
return [[JSQMessagesTimestampFormatter sharedFormatter] attributedTimestampForDate:message.date];
}
}
,最终代码为:
gulp build
注意:我在项目中使用DateTools。
答案 2 :(得分:1)
谢谢@cerenali的回答。
在 Swift-3 中,@ cerenali代码可以写成: -
override func collectionView(_ collectionView: JSQMessagesCollectionView, attributedTextForCellTopLabelAt indexPath: IndexPath) -> NSAttributedString? {
let message = self.messages[indexPath.item]
if indexPath.item == 0 {
return JSQMessagesTimestampFormatter.shared().attributedTimestamp(for: message.date)
}
if indexPath.item - 1 > 0{
let previousMessage = self.messages[indexPath.item - 1 ]
if ( ( message.date.timeIntervalSince(previousMessage.date) / 60 ) > 1){
return JSQMessagesTimestampFormatter.shared().attributedTimestamp(for: message.date)
}
}
return nil
}
override func collectionView(_ collectionView: JSQMessagesCollectionView, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout, heightForCellTopLabelAt indexPath: IndexPath) -> CGFloat {
if indexPath.item == 0 {
return kJSQMessagesCollectionViewCellLabelHeightDefault
}
if indexPath.item - 1 > 0{
let message = self.messages[indexPath.item]
let previousMessage = self.messages[indexPath.item - 1 ]
if ( ( message.date.timeIntervalSince(previousMessage.date) / 60 ) > 1){
return kJSQMessagesCollectionViewCellLabelHeightDefault
}
}
return 0.0
}