2014-01-14 28 views
1

我需要创建一个聊天记录,如whatsapp,即:我想拥有与对话一样多的单元格,并且每个单元格都必须显示用户名进行对话,并在对话中的最后一条消息。在MySQL数据库有一个表(聊天)具有以下字段:创建类似于whatsapp的消息历史(objective-c)

  • ID
  • IDConversation(它表示的会话的ID,从而为在单个会话联合,具有相同IDConversation的所有消息)
  • IDSender(它代表谁发送的消息)
  • IDReceiver(它代表谁接收的消息)
  • 发送者姓名(它代表谁发送了该用户的名称的用户的ID的用户的ID消息)
  • ReceiverName(它代表谁接收消息的用户的名称)
  • 消息(它表示在会话中的发送的消息)
  • 日期时间(它表示发送的消息的日期时间,在格式YYYY- MMM-DD)

以我的UITableViewController类我已经用来填充表(本身份识别码是包含登录该时刻的用户的ID的字符串的方法 - _script是发送查询一个类的实例到php脚本并连接到mysql数据库):

- (void)getConversations{ 
     _arrID = [[NSMutableArray alloc] initWithArray:[_script objsRequest:[NSString stringWithFormat:@"SELECT ID FROM Chat WHERE IDSender = %@ OR IDReceiver = %@",_myID,_myID]]]; 
     _arrLastID = [[NSMutableArray alloc] init]; 
     _arrLastMessages = [[NSMutableArray alloc] init]; 
     _arrIDConversations = [[NSMutableArray alloc] init]; 
     _arrIDUsers = [[NSMutableArray alloc] init]; 
     _arrProfileSnaps = [[NSMutableArray alloc] init]; 
     _arrUsernames = [[NSMutableArray alloc] init]; 
    [_arrLastID addObjectsFromArray:[_script objsRequest:[NSString stringWithFormat:@"SELECT MAX(ID) FROM Chat WHERE IDSender = %@ OR IDReceiver = %@",_myID,_myID]]]; 
    for (int i = 0; i <[_arrLastID count]; i++) { 
      NSString *IDUser = [_script objRequest:[NSString stringWithFormat:@"SELECT IDSender FROM Chat WHERE ID = %@ AND IDReceiver = %@",_arrLastID[i],_myID]]; 
      if ([IDUser isEqualToString:@""]) { 
      IDUser = [_script objRequest:[NSString stringWithFormat:@"SELECT IDReceiver FROM Chat WHERE ID = %@ AND IDSender = %@",_arrLastID[i],_myID]]; 
      } 
      [_arrIDUsers addObject:IDUser]; 
      [_arrLastMessages addObject:[_script objRequest:[NSString stringWithFormat:@"SELECT Message FROM Chat WHERE ID = %@",_arrLastID[i]]]]; 
      [_arrIDConversations addObject:[_script objRequest:[NSString stringWithFormat:@"SELECT IDConversation FROM Chat WHERE ID = %@",_arrLastID[i]]]]; 
      [_arrUsernames addObject:[NSString stringWithFormat:@"%@ %@",[_script objRequest:[NSString stringWithFormat:@"SELECT Name FROM User WHERE ID = %@",IDUser]],[_script objRequest:[NSString stringWithFormat:@"SELECT Surname FROM User WHERE ID = %@",IDUser]]]]; 

     [self.tableView reloadData]; 
    } 


     - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     return 1; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     return [_arrIDConversations count]; 

    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 


     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
     cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"userCell"]]; 

     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 
     } 

      NSString *username = [self.arrUsernames objectAtIndex:indexPath.row]; 
      NSString *lastMessage = [self.arrLastMessages objectAtIndex:indexPath.row]; 
      NSString *IDUser = [self.arrIDUsers objectAtIndex:indexPath.row]; 

      UILabel *cellLabelUsername = (UILabel *)[cell viewWithTag:1]; 
      NSMutableAttributedString *richTask = [[NSMutableAttributedString alloc] 
                initWithString:[NSString stringWithFormat:@"%@",username]]; 
      cellLabelUsername.attributedText = richTask; 

UILabel *cellLabelLastMessage = (UILabel *)[cell viewWithTag:3]; 
      cellLabelLastMessage.text = lastMessage; 

     } 

我知道在阅读对话(getConversations)的代码时出现错误,因为一旦我启动应用程序,只有一个单元格包含最后一次对话和最后一条消息,但不会与其他相关文章进行对话。

请帮帮我!

回答