它的可能更改逻辑为collectionView:attributedTextForCellTopLabelAtIndexPath:showate方法,显示日期时间戳不是由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; }
当前存在的逻辑显示相同的时间戳重复如下.
解决方法
因为每个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; }