Iphone NSMutableArray未更新,UITableView未重新加载
我正在创建一个聊天应用程序。我的视图控制器中有两种方法,一种用于发送消息,另一种用于接收消息。在发送方法中,我创建了一个包含两个对象的NSMutableDictionaryIphone NSMutableArray未更新,UITableView未重新加载,iphone,objective-c,uitableview,nsmutablearray,nsmutabledictionary,Iphone,Objective C,Uitableview,Nsmutablearray,Nsmutabledictionary,我正在创建一个聊天应用程序。我的视图控制器中有两种方法,一种用于发送消息,另一种用于接收消息。在发送方法中,我创建了一个包含两个对象的NSMutableDictionary NSMutableDictionary *msgFilter = [[NSMutableDictionary alloc] init]; [msgFilter setObject:messageStr forKey:@"msg"]; [msgFilter setObject:@"you" forKey:@"sender"];
NSMutableDictionary *msgFilter = [[NSMutableDictionary alloc] init];
[msgFilter setObject:messageStr forKey:@"msg"];
[msgFilter setObject:@"you" forKey:@"sender"];
[messages addObject:msgFilter];
“messages”是我的主要NSMutableArray,用于保存所有消息,其属性是设置、合成和分配的。当我发送消息时,它被正确地添加到NSMutableArray中,并且UITableView会更新,显示单元格中的值
我在appDelegate中有一个方法来检查收到的消息,并使用相同的过程来解析数据并将其存储在NSMutableDictionary中。然后将该字典传递给viewcontroller并添加到相同的NSMutableArray(消息)中,然后调用[self.chattable reloadData]。但这没什么用。当我记录NSMutableArray时,它只有收到的消息,而不是整个数据(发送+接收)
为什么不将收到的消息添加到同一个数组中,为什么不刷新我的表。我已经试着让它工作好几天了…请帮忙。。
//接收信息部分
NSMutableDictionary *msgFilter = [myDelegate msgFilter];
[messages addObject:msgFilter];
[self.tView reloadData];
//表视图部分
- (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;
}
在应用程序委托中声明消息数组。所以它将是共享阵列。所以你的问题可能会得到解决。因为它是共享数组。所以,您可以在消息数组中的任何位置添加字典,而无需在diff UIView之间传递字典 你能在添加收到的消息和与tableview相关的代码的地方发布代码吗?@Hanon,我已经更新了帖子。似乎一切正常,你确定self.tView不是零吗?在重新加载data.2012-03-12 16:33:05.687 Montact1[11193:207]消息之前放置一个日志({msg=334;sender=you;})2012-03-12 16:33:05.691 Montact1[11193:207]Sent({msg=334;sender=you;})当我按下send按钮时,数据显示在tableview中。调用receive message方法时,只是接收到的消息没有显示或添加到NSMutableArray@fbernardo中。数据添加到NSMutableArray中,但其中没有发送消息。只有最后收到的消息…它应该都在NSMutableArray中。@XYZ.great tip。我尝试了。但在发送或接收消息时,表没有重新加载。我将此添加到我的appldelegate NSMutableDictionary*msgFilter=[[NSMutableDictionary alloc]init];[msgFilter setObject:msg forKey:@“msg”];[msgFilter setObject:@“您”forKey:@“发件人”];[消息添加对象:msgFilter];[fbChatView.tView重新加载数据];不自动重新加载tableview。如果按按钮调用重新加载,它将加载[msgFilter setObject:msg forKey:@“msg”];[msgFilter setObject:@“您”forKey:@“发件人”];[消息添加对象:msgFilter];[fbChatView.tView重新加载数据];