我正在创建一个聊天应用程序。我的视图控制器中有两个方法用于发送,一个用于接收消息。在发送方法中,我创建了一个带有两个对象的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中。然后将该字典传递给viewcontroller并添加到相同的NSMutableArray(消息)中,然后调用[self.chattable reloadData]。但这没有做任何事情。当我使用NSMutableArray时,它只收到的消息不是整个数据(发送+收到)。
// Recives message section
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;
}
答案 0 :(得分:0)
在Application Delegate中声明消息数组。所以它将是共享数组。所以可能是你的问题得到解决。因为它是共享数组。所以你可以在任何地方的messages数组中添加Dictionary,不需要在diff UIView之间传递字典。