【发布时间】:2012-03-12 07:53:19
【问题描述】:
我正在创建一个聊天应用程序。我的视图控制器中有两种方法,一种用于发送消息,另一种用于接收消息。在发送方法中,我创建了一个带有两个对象的 NSMutableDictionary ..
NSMutableDictionary *msgFilter = [[NSMutableDictionary alloc] init];
[msgFilter setObject:messageStr forKey:@"msg"];
[msgFilter setObject:@"you" forKey:@"sender"];
[messages addObject:msgFilter];
“messages”是我的主要 NSMutableArray 用于保存所有消息,其属性已设置、合成和分配。当我发送消息时,它会正确添加到 NSMutableArray 中,并且 UITableView 会更新,向我显示单元格中的值。
我的 appDelegate 中有一个方法来检查收到的消息并使用相同的过程来解析数据并将其存储在 NSMutableDictionary 中。然后将该字典传递给视图控制器并添加到相同的 NSMutableArray(messages) 中,然后我调用 [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;
}
【问题讨论】:
-
能贴出添加收到消息的代码和tableview相关的代码吗?
-
@Hanon,我已经更新了帖子..
-
一切正常,你确定 self.tView 不是 nil 吗?在 reloadData 之前放置一个日志。
-
2012-03-12 16:33:05.687 Montact1[11193:207] TX 中的消息 GOT ({ msg = 334; sender = you; }) 2012-03-12 16:33:05.691 Montact1[11193:207] 已发送({ msg = 334; sender = you; })当我按下发送按钮时,数据显示在表格视图中。它只是收到的消息没有显示或添加到 NSMutableArray
-
@fbernardo 当调用接收消息方法时,数据被添加到 NSMutableArray 中,但其中没有发送消息。只有最后收到的消息...它应该都在 NSMutableArray 中。
标签: iphone objective-c uitableview nsmutablearray nsmutabledictionary