我需要创建一个聊天记录,例如whatsapp,即:我想拥有与对话一样多的单元格,每个单元格必须显示用于执行对话的用户名,以及对话中的最后一条消息。在mySQL DB中有一个表(Chat),其中包含以下字段:
在我的UITableViewController类中,我使用了用于填充表的方法(myID是一个包含当时登录用户的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),因为一旦我启动应用程序,只有一个单元格有最后一个会话和最后一条消息,但是没有出现与其他相关帖子的对话
请帮助我!