Note that there are some explanatory texts on larger screens.

plurals
  1. POTableView not reloading when data is inserted into source from different source
    primarykey
    data
    text
    <p>I have a ChatViewController where I send and receive messages and store the messages as a NSMutableDictionary in an NSArray. </p> <pre><code>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]); } </code></pre> <p>When I am calling 'addMessage' from AppDelegate, both the strings are passed but it cannot add it to the 'messages' because the 'Number of rows' always is zero. But when I am storing it from that class itself then the messages are getting stored and row count increases. </p> <p>As a result, it only shows the messages from the ChatViewController but not the messages sent from the AppDelegate. I hope I was able to explain the problem properly.</p> <p>Here is the cellForRowAtIndexPath function:</p> <pre><code>- (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; } </code></pre> <p>Yes, I have initialized the messages array in viewDidLoad. But the problem is when I am trying to insert into the messages array from the AppDelegate the count is not increasing. In fact, it always shows zero. But when I insert from the ViewController itself, it maintains the count. Here is the AppDelegate code:</p> <pre><code>- (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]; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload