0
votes

I'm creating a chat application. I have 2 methods in my view controller one for sending and one for receiving the messages. In the sending method i create a NSMutableDictionary with two objects ..

NSMutableDictionary *msgFilter = [[NSMutableDictionary alloc] init];
[msgFilter setObject:messageStr forKey:@"msg"];
[msgFilter setObject:@"you" forKey:@"sender"];

[messages addObject:msgFilter];

"messages" is my main NSMutableArray for holding all the messages, whose property is set and synthesized and allocated. When i send the message it is properly added into the NSMutableArray and the UITableView is updated showing me the values in the cell.

I have a method in my appDelegate to check for messages received and use the same procedure to parse the data and store it in an NSMutableDictionary. This dictionary is then passed to the viewcontroller and added into the same NSMutableArray(messages) and i then call [self.chattable reloadData]. But this doesn't do anything. When i nsloged the NSMutableArray it only had the received message not the whole data(send + received).

Why is it not adding the received messages into the same array and why is it not refreshing my table. I've been trying to get it to work for days now...Plz help..

//Recives message section

NSMutableDictionary *msgFilter = [myDelegate msgFilter];
[messages addObject:msgFilter];
[self.tView reloadData];

//Tableview section

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [messages count];    
 }

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {   
return 1;   
  }
   -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath 
  {
static NSString *CellIdentifier = @"Cell";       
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{        
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *s = (NSDictionary *) [messages objectAtIndex:indexPath.row];
NSString *sender = [s objectForKey:@"sender"];
NSString *message = [s objectForKey:@"msg"];

if ([sender isEqualToString:@"you"])
{
    cell.detailTextLabel.text = [NSString stringWithFormat:@"TX: %at", message];
}
else
{
    cell.detailTextLabel.text = [NSString stringWithFormat:@"RX: %at", message];
}    
return cell;
  }
1
can you post the code where you add the received message and the code related to the tableview? - Hanon
@Hanon, I've update the post.. - sansid1983
Everything seems right, are you sure self.tView is not nil? Place a log before the reloadData. - fbernardo
2012-03-12 16:33:05.687 Montact1[11193:207] Message GOT in TX ( { msg = 334; sender = you; } ) 2012-03-12 16:33:05.691 Montact1[11193:207] Sent ( { msg = 334; sender = you; } ) When i press the send button the data is showing in the tableview. Its just the message received not showing or adding into the NSMutableArray - sansid1983
@ fbernardo when the receive message method is called the data is added into the NSMutableArray,but it does not have the send messages in it. Only has the last received message...its should be both in NSMutableArray. - sansid1983

1 Answers

0
votes

Declare messages array in Application Delegate. so it will be shared array. so might be your problem get solved. because it is shared array. so you can add Dictionary in messages array from any where, no need to pass dictionary between diff UIView.