I have two classes, class A and class B. Class A has a mission not related to chat BUT it needs to react to the arrive of a chat message. When a message arrives, it is stored in a NSMutableDictionary, being that dictionary stored in a NSMutableArray. If the message received is the first one ([array count]==1), class B view is displayed, presenting an UITableView, wich data is populated with the array. Everything works perfectly for the fierst message.
A second message arrives. The new message is added to the array (that is done in class A) but, the table is not populated. How could i achieve that goal?
The code:
Class A
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
NSString *msg = [[message elementForName:@"body"] stringValue];
NSString *from = [[message attributeForName:@"from"] stringValue];
NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
[m setObject:msg forKey:@"msg"];
[m setObject:from forKey:@"sender"];
[messarray addObject:m];
[self newMessageReceived:messarray];
}
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
NSLog(@"Method xmppStreamDidAuthenticate");
[self goOnline];
}
//- (void) newMessageReceived:(NSMutableDictionary *)message
-(void) newMessageReceived:(NSMutableArray *)messarraylocal
{
General *general = [General sharedManager];//An static class to store settings like username, and other things that must be available in the hole app.
general.numcels++;
general.messarray=messarraylocal;
// NSString *firstmessage=[message objectForKey:@"msg"];
//NSString *from=[message objectForKey:@"sender"];
//NSArray *listItems = [from componentsSeparatedByString:@"@"];
//NSString *fromsplit=[listItems objectAtIndex:0];
//general.firstmess=firstmessage;
//general.firstfrom=fromsplit;
if ([messarraylocal count]==1){
ChatViewController *cvc=[[ChatViewController alloc]initWithNibName:@"Chat" bundle:nil];
[[self navigationController]pushViewController:cvc animated:YES];
}
}
Class B:
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
General *general = [General sharedManager];
NSLog(@"We are in numberOfRowsInSection, de ChatViewController");
return general.messarray.count;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
General *general = [General sharedManager];
NSLog(@"We are in cellForRowAtIndexPath de ChatViewController");
//UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
//if(!cell){
messarray=general.messarray;
for (int i=0;i<messarray.count; i++)
{
diccio=[messarray objectAtIndex:i];
general.firstmess=[diccio objectForKey:@"msg"];
general.firstfrom=[diccio objectForKey:@"sender"];
UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
//}
text=general.firstmess;
remite=general.firstfrom;
[[cell textLabel]setText:remite];
[[cell detailTextLabel] setText:text];
return cell;
}
}
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
NSString *msg = [[message elementForName:@"body"] stringValue];
NSString *from = [[message attributeForName:@"from"] stringValue];
NSLog(@"We are in didReceiveMessage of ChatViewController");
NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
[m setObject:msg forKey:@"msg"];
[m setObject:from forKey:@"sender"];
[self newMessageReceived:m];
}
- (void) newMessageReceived: (NSMutableDictionary *) message// this method is NEVER called
{
General *general = [General sharedManager];
general.numcels++;
NSLog(@"numcels vale %i", general.numcels);
NSString *firstmessage=[message objectForKey:@"msg"];
NSString *from=[message objectForKey:@"sender"];
NSArray *listItems = [from componentsSeparatedByString:@"@"];
NSString *fromsplit=[listItems objectAtIndex:0];
general.firstmess=firstmessage;
general.firstfrom=fromsplit;
NSLog(@"Message receive in ChatViewController: %@ from %@", [message objectForKey:@"msg"], fromsplit);
}
In the method didViewLoad of class B, i do:
table.delegate=self;
table.dataSource=self;
But with no result. I know that [table reload] could work but… where can i do it?
Anybody can help me? Thank you!
In class A
newMessageReceivedafter you add the new message to the globalmessarrayYou will need to fire an
NSNotificationNow in Class B register for observation