Pinturikkio
Pinturikkio

Reputation: 1500

Create a message history similar to whatsapp (objective-c)

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:

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):

- (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;

        }

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.

Please help me!

Upvotes: 1

Views: 2146

Answers (1)

Luca Iaco
Luca Iaco

Reputation: 3457

this git project could be useful:

https://github.com/andreamazz/AMBubbleTableView/

Upvotes: 2

Related Questions