我有两个班级,A班和B班。班级A的任务与聊天无关,但需要对聊天消息的到达作出反应。当消息到达时,它存储在NSMutableDictionary中,即存储在NSMutableArray中的字典。如果收到的消息是第一个([数组计数] == 1),则显示B类视图,显示UITableView,数据将填充数组。一切都适合fierst消息。
第二条消息到来。新消息将添加到数组中(在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]可以工作但是......我在哪里可以做到?
任何人都可以帮助我吗?谢谢!
答案 0 :(得分:0)
在将新邮件添加到全局newMessageReceived
messarray
中
您需要触发NSNotification
-(void) newMessageReceived:(NSMutableArray *)messarraylocal
{
//in addition to your normal code, add the following
[[NSNotificationCenter defaultCenter] postNotificationName:@"NewMessage" object:nil];
}
现在在B级注册中进行观察
- (void) viewDidLoad
{
//In addition to your normal code here add the following
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newMessage) name:@"NewMessage" object:nil];
}
//Add this method that will call reload data
- (void) newMessage
{
[table reloadData];
}
答案 1 :(得分:0)
您是否将以前的消息存储在数组中... 如果没有,那么在 didReceiveMessage:中添加一行,然后在数组中添加对象...
[messarray removeAllObjects];