我正在使用JSQMessagesViewController从服务器发送和接收消息。消息显示正常,但在尝试显示日期时,所有消息都会出现,然后在JSQMessagesTimestampFormatter Library Class上崩溃。这是 - (NSAttributedString *)attributionTimestampForDate:(NSDate *)date
由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' NSConcreteMutableAttributedString initWithString:attributes :: nil value'
- (NSAttributedString *)attributedTimestampForDate:(NSDate *)date
{
if (!date) {
return nil;
}
NSString *relativeDate = [self relativeDateForDate:date];
NSString *time = [self timeForDate:date];
// TimeStamp
在线下获得崩溃NSMutableAttributedString *timestamp = [[NSMutableAttributedString alloc] initWithString:relativeDate
attributes:self.dateTextAttributes];
[timestamp appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
[timestamp appendAttributedString:[[NSAttributedString alloc] initWithString:time
attributes:self.timeTextAttributes]];
return [[NSAttributedString alloc] initWithAttributedString:timestamp];
}
我的代码
//获取客户聊天消息
- (void)getCustomerChatMessages
{
[[ServiceManager sharedInstance]GetChatMessagesforParentSK:self.str_ChatMessageSK completionBlock:^(id result, NSError *error)
{
if (!error)
{
NSDictionary *recallsDict = (NSDictionary *)result;
self.array_CustChatMessage = [recallsDict valueForKey:@"ChatMessageData"];
for (NSArray *dict in self.array_CustChatMessage)
{
JSQMessage * copyMessage = [[JSQMessage alloc] initWithSenderId:[[dict valueForKey:@"FromSK"] stringValue]
senderDisplayName:[dict valueForKey:@"FirstName"]
date:[dict valueForKey:@"DateCreated"]
text:[dict valueForKey:@"Message"]];
[self.messages addObject:copyMessage];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self finishSendingMessageAnimated:YES];
});
}
}];
}
- (NSAttributedString *)collectionView:(JSQMessagesCollectionView *)collectionView attributedTextForCellTopLabelAtIndexPath:(NSIndexPath *)indexPath
{
/**
* This logic should be consistent with what you return from `heightForCellTopLabelAtIndexPath:`
* The other label text delegate methods should follow a similar pattern.
*
* Show a timestamp for every 3rd message
*/
{
JSQMessage *message = [self.messages objectAtIndex:indexPath.item];
NSLog(@"\n\n\n Message Date :%@",message.date);
return [[JSQMessagesTimestampFormatter sharedFormatter] attributedTimestampForDate:message.date];
}
return nil;
}
- (NSAttributedString *)collectionView:(JSQMessagesCollectionView *)collectionView attributedTextForMessageBubbleTopLabelAtIndexPath:(NSIndexPath *)indexPath
{
JSQMessage *message = [self.messages objectAtIndex:indexPath.item];
/**
* iOS7-style sender name labels
*/
if ([message.senderId isEqualToString:self.senderId])
{
return nil;
}
if (indexPath.item - 1 > 0)
{
JSQMessage *previousMessage = [self.messages objectAtIndex:indexPath.item - 1];
if ([[previousMessage senderId] isEqualToString:message.senderId]) {
return nil;
}
}
/**
* Don't specify attributes to use the defaults.
*/
NSLog(@"Sender Display Name:%@",[[NSAttributedString alloc] initWithString:self.strSenderName]);
return [[NSAttributedString alloc] initWithString:self.strSenderName];
}