我有两个班级,A 班和 B 班。A 班的任务与聊天无关,但它需要对收到的聊天消息做出反应。当消息到达时,它被存储在 NSMutableDictionary 中,即存储在 NSMutableArray 中的字典。如果收到的消息是第一个消息([array count]==1),则显示 B 类视图,呈现 UITableView,其中数据填充有数组。一切都非常适合第一条消息。
第二条消息到达。新消息被添加到数组中(这是在 A 类中完成的),但是表没有被填充。我怎样才能实现这个目标?
编码:
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];
}
}
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);
}
在 B 类的方法 didViewLoad 中,我这样做:
table.delegate=self;
table.dataSource=self;
但是没有结果。我知道 [table reload] 可以工作,但是......我在哪里可以做到?
任何人都可以帮助我吗?谢谢!