Title: How to show archived message from core data in to the chat tableview?
Tags: ios;objective-c;arrays;uitableview;xmppframework
Question: I am able to get the archived messages. but I can't figure out how to show it in the tableview as I have already showing the online chat with it. so can anyone help me how to show the old messages before online chat to load it in the tableview?
Here I am getting the archived messages:
-(void)print:(NSMutableArray*)messages_arc{
@autoreleasepool {
for (XMPPMessageArchiving_Message_CoreDataObject *message in messages_arc) {
// NSString *server=@"@www.demoseocousa.com";
// chatWithUser=[chatWithUser stringByAppendingString:server];
NSXMLElement *element = [[NSXMLElement alloc] initWithXMLString:message.messageStr error:nil];
NSLog(@"to param is %@",[element attributeStringValueForName:@"to"]);
s = [[NSMutableDictionary alloc] init];
[s setObject:message.body forKey:@"msg"];
if ([[element attributeStringValueForName:@"to"] isEqualToString:chatWithUser]) {
[s setObject:sender forKey:@"sender"];
}
else
{
[s setObject:chatWithUser forKey:@"sender"];
}
[messages addObject:s];
// NSString *t=message.timestamp
// [s setObject:t forKey:"time"];
NSLog(@"bareJid param is %@",message.bareJid);
NSLog(@"bareJidStr param is %@",message.bareJidStr);
NSLog(@"body param is %@",message.body);
NSLog(@"timestamp param is %@",message.timestamp);
NSLog(@"outgoing param is %d",[message.outgoing intValue]);
NSLog(@"***************************************************");
messages=messages_arc;
And this is where I am populating the tableView for online chat:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
static NSString *CellIdentifier = @"MessageCellIdentifier";
UIImage *bgImage = nil;
SMMessageViewTableCell *cell = (SMMessageViewTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[SMMessageViewTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
NSDictionary *x = (NSDictionary *) [messages objectAtIndex:indexPath.row];
NSString *sender2 = [x objectForKey:@"sender"];
NSLog(@"CHATWITHUSER : %@",chatWithUser);
NSString *message = [x objectForKey:@"msg"];
NSString *time = [x objectForKey:@"time"];
CGSize  textSize = { 260.0, 10000.0 };
CGSize size2 = [message sizeWithFont:[UIFont boldSystemFontOfSize:12]
constrainedToSize:textSize
lineBreakMode:UILineBreakModeWordWrap];
size2.width += (padding/2);
cell.messageContentView.text = message;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.userInteractionEnabled = YES;
NSLog(@"CHATWITH %@,",chatWithUser);
if ([sender2 isEqualToString:chatWithUser])
{ // left aligned
bgImage = [[UIImage imageNamed:@"03.png"] stretchableImageWithLeftCapWidth:30  topCapHeight:19];
[cell.messageContentView setFrame:CGRectMake(padding, padding*2, size2.width, size2.height)];
[cell.bgImageView setFrame:CGRectMake( cell.messageContentView.frame.origin.x - padding/2,
cell.messageContentView.frame.origin.y - padding/2,
size2.width+padding,
size2.height+padding)];
if ([sender2 isEqualToString:[[[[self appDelegate] xmppStream] myJID] bare]]) {
bgImage = [[UIImage imageNamed:@"Aqua.png"] stretchableImageWithLeftCapWidth:24  topCapHeight:10];
[cell.messageContentView setFrame:CGRectMake(320 - size2.width - padding,
padding*2,
size2.width,
size2.height)];
[cell.bgImageView setFrame:CGRectMake(cell.messageContentView.frame.origin.x - padding/2,
cell.messageContentView.frame.origin.y - padding/2,
size2.width+padding,
size2.height+padding)];
cell.bgImageView.image = bgImage;
//   cell.textLabel.text=[[messages objectAtIndex:indexPath.row] objectForKey:@"msg"];
cell.senderAndTimeLabel.text = [NSString stringWithFormat:@"%@ %@", sender2, time];
NSLog(@"The MEssage==&gt;%@",messages);
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
return cell;
So please suggest me how to do it and where should i reloadData to tableView as here I am loading it in viewDidLoad ?
Comment: @Sushil here online chat means when both users are online and chatting.

Comment: @Sushil yeah at the beginning

Comment: @Sushil the problem is that as the array I am getting for archived message is of type 'XMPPMessageArchiving_Message_CoreDataObject' and for instant message the array I am getting is of type 'XMPPMessage'

Comment: I actually didn't understand what you are asking. What is online chat?

Comment: So you want to show old chat?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:06/08/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><objective-c><arrays><uitableview><xmppframework>', 'Title': 'How to show archived message from core data in to the chat tableview?', 'ViewCount': 179, 'id': 30701028, 'user_AboutMe': '<p>I am an android and iOS app developer.</p>\n', 'user_AccountId': 5652361.0, 'user_DisplayName': 'Sushrita', 'user_Reputation': 715.0, 'user_Views': 223.0, 'user_id': 4472486.0}}