简体   繁体   中英

NSMutableArray not updating and UITableView not reloading

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;
  }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM