0

我有一个 ChatViewController,我在其中发送和接收消息,并将消息作为 NSMutableDictionary 存储在 NSArray 中。

NSMutableArray *messages; //in header file

- (void)addMessage:(NSString *)receivedMessage :(NSString *) sender
{
    [self reloadDataInTableView:receivedMessage :sender];
}

- (void)reloadDataInTableView:(NSString *)message :(NSString*)sender
{
    NSLog(@"Text: %@ Sender: %@", message, sender);
    NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
    [m setObject:message forKey:@"msg"];
    [m setObject:sender forKey:@"sender"];
    [messages addObject:m];
    [self.tView reloadData];
    NSLog(@"Number of rows: %d", [messages count]);
}

当我从 AppDelegate 调用“addMessage”时,两个字符串都被传递,但它无法将其添加到“消息”中,因为“行数”始终为零。但是当我从那个类本身存储它时,消息被存储并且行数增加。

因此,它只显示来自 ChatViewController 的消息,而不显示来自 AppDelegate 的消息。我希望我能够正确解释这个问题。

这是 cellForRowAtIndexPath 函数:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSMutableDictionary *s = (NSMutableDictionary *) [messages objectAtIndex:indexPath.row];
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        //cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [s objectForKey:@"msg"];
    cell.detailTextLabel.text = [s objectForKey:@"sender"];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.userInteractionEnabled = NO;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [messages count];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

是的,我已经在 viewDidLoad 中初始化了消息数组。但问题是当我试图从 AppDelegate 插入消息数组时,计数没有增加。事实上,它总是显示为零。但是当我从 ViewController 本身插入时,它会保持计数。这是 AppDelegate 代码:

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
    if ([message isChatMessageWithBody])
    {
        XMPPUserCoreDataStorageObject *user = [xmppRosterStorage userForJID:[message from]
                                                                 xmppStream:xmppStream
                                                       managedObjectContext:[self managedObjectContext_roster]];

        NSString *messageBody = [[message elementForName:@"body"] stringValue];
        NSString *displayName = [user jidStr];

        if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
        {
            ChatViewController *cvc = [[ChatViewController alloc] init];
            [cvc reloadDataInTableView:messageBody :displayName];
        }
        else
        {
            // We are not active, so use a local notification instead
            UILocalNotification *localNotification = [[UILocalNotification alloc] init];
            localNotification.alertAction = @"Ok";
            localNotification.alertBody = [NSString stringWithFormat:@"From: %@\n\n%@",displayName,messageBody];

            [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
        }
    }
}
4

3 回答 3

1

当你这样做

ChatViewController *cvc = [[ChatViewController alloc] init];
[cvc reloadDataInTableView:messageBody :displayName];

在您的应用程序委托中,您正在实例化一个新的ChatViewController. 即使messages数组是在 init 方法中初始化的(它不是,正如你所说的那样它是在viewDidLoad方法中初始化的),这个新的实例ChatViewController在你的方法结束时被丢弃,xmppStream:didReceiveMessage:并且绝对与显示的不同在屏幕上。您需要ChatViewController在您的应用程序委托中保留对正确实例的引用并使用它而不是实例化一个新实例...

如果您需要有关如何解决此问题的更具体建议,您需要向我们提供有关整个项目架构的更多详细信息(使用故事板?是否有导航控制器?选项卡控制器?chatviewcontroller 与其他视图控制器的关系如何? ? 等等)。

于 2013-06-25T08:55:50.567 回答
0

你需要

messages = [[NSMutableArray alloc] init];

viewdidload

于 2013-06-25T08:21:25.620 回答
-1

尝试这个:

- (void)reloadDataInTableView:(NSString *)message :(NSString*)sender
{
    NSLog(@"Text: %@ Sender: %@", message, sender);
    NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
    [m setObject:message forKey:@"msg"];
    [m setObject:sender forKey:@"sender"];
    if (!messages)
    {
        messages = [[NSMutableArray alloc] init];
    }
    [messages addObject:m];
    [self.tView reloadData];
    NSLog(@"Number of rows: %d", [messages count]);
}
于 2013-06-25T08:19:08.220 回答