【发布时间】:2015-12-18 23:13:25
【问题描述】:
我正在用 xmpp 框架制作一个聊天应用程序。
我已经通过引用此链接在我的项目中设置了 XMPPFramework:- http://code.tutsplus.com/tutorials/building-a-jabber-client-for-ios-xmpp-setup--mobile-7190
在此消息发送中工作正常,在接收消息中它显示在 alertview 中,就像它在代码中一样。当收到消息时,此方法正在调用,它位于 Appdelegate.m 文件中:-
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{ DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
// A simple example of inbound message handling.
if ([message isChatMessageWithBody])
{
XMPPUserCoreDataStorageObject *user = [xmppRosterStorage userForJID:[message from]
xmppStream:xmppStream
managedObjectContext:[self managedObjectContext_roster]];
NSString *body = [[message elementForName:@"body"] stringValue];
NSString *displayName = [user displayName];
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:displayName
message:body
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}
else
{
// We are not active, so use a local notification instead
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertAction = @"Ok";
localNotification.fireDate=[NSDate dateWithTimeIntervalSinceNow:1.0];
localNotification.alertBody = [NSString stringWithFormat:@"From: %@\n%@",displayName,body];
NSLog(@"localNotification.alertBody:-%@",localNotification.alertBody);
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
}
}
我想在另一个类中的对话视图(在表格视图中)中显示收到的消息。
这里是在 tableview 中显示发送消息的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=nil;
cell=[tableView dequeueReusableCellWithIdentifier:@"MessageCellIdentifier" forIndexPath:indexPath];
cell.textLabel.text=[[messages objectAtIndex:indexPath.row] objectForKey:@"msg"];
cell.detailTextLabel.text=@"You";
cell.textLabel.textAlignment=NSTextAlignmentRight;
return cell;
}
所以,我的问题是如何将接收到的消息放入数组或字典中,并将其带到 tableview 单元格中,以便 indexpath 中的行显示在 tableview 中。
【问题讨论】:
-
使用通知器 ....并发布和添加通知器以在您的 tableview 控制器中获取数据
-
我没有正确理解。请你用代码解释一下
-
我已经引用了这个链接stackoverflow.com/questions/30844763/…
-
是的,你可以按照那个...因为他使用的方式和我告诉你的一样
-
是的,我现在在 tableview 中收到消息要左右对齐...
标签: ios objective-c xcode uitableview xmppframework