【问题标题】:NSMutableArray not updating and UITableView not reloadingNSMutableArray 未更新且 UITableView 未重新加载
【发布时间】: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


【解决方案1】:

在 Application Delegate 中声明消息数组。所以它将是共享数组。所以你的问题可能会得到解决。因为它是共享数组。因此您可以从任何地方在消息数组中添加字典,无需在 diff UIView 之间传递字典。

【讨论】:

  • @XYZ.great 提示。我试过了。但是当我发送消息或接收它时,表没有重新加载。我将这个添加到我的 appldelegate NSMutableDictionary *msgFilter = [[NSMutableDictionary alloc] init] ; [msgFilter setObject:msg forKey:@"msg"]; [msgFilter setObject:@"you" forKey:@"sender"]; [消息添加对象:msgFilter]; [fbChatView.tView reloadData];不自动重新加载 tableview。如果我通过按下按钮调用重新加载,它会加载 [msgFilter setObject:msg forKey:@"msg"]; [msgFilter setObject:@"you" forKey:@"sender"]; [消息添加对象:msgFilter]; [fbChatView.tView reloadData];
猜你喜欢
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-08
  • 1970-01-01
  • 2013-06-22
  • 2020-02-24
  • 1970-01-01
相关资源
最近更新 更多