title: Create a message history similar to whatsapp (objective-c)
body: <p>I need to create a chat history such as whatsapp, that is: I wanna have as many cells as there are conversations and each cell must show the user name with which you performed the conversation, and the last message in the conversation. In mySQL DB there is a table (Chat) with the following fields:</p>
  
  <ul>
  <li>ID</li>
  <li>IDConversation (it represents the ID of the conversation, so as to unite in a single conversation, all messages with the same IDConversation)</li>
  <li>IDSender (it represents the ID of the user who sent the message)</li>
  <li>IDReceiver (it represents the ID of the user who received the message)</li>
  <li>SenderName (it represents the name of the user who sent the message)</li>
  <li>ReceiverName (it represents the name of the user who received the message)</li>
  <li>Message (it represents the sent message in the conversation)</li>
  <li>DateTime (it represents the date-time of the message sent, in the format yyyy-mmm-dd)</li>
  </ul>
  
  <p>In my UITableViewController class I've the method used to populate the table (myID is a string containing the ID of the user logged on that time - _script is an instance of a class that sends queries to php script and connect to mysql db):</p>
  
  <pre><code>- (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 &lt;[_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;
  
          }
  </code></pre>
  
  <p>I know that there is something wrong in the code of reading the conversations (getConversations) because once I start the app there is only one cell with the last conversation and the last message, but don't appear conversations with other related posts.</p>
  
  <p>Please help me!</p>