【发布时间】:2014-01-14 01:07:23
【问题描述】:
我需要创建一个聊天记录,例如 whatsapp,即:我希望拥有与对话数量一样多的单元格,并且每个单元格都必须显示您进行对话时使用的用户名,以及对话中的最后一条消息。在 mySQL DB 中有一个表(聊天),其中包含以下字段:
- 身份证
- IDConversation(表示会话的ID,以便统一在一个会话中,所有具有相同IDConversation的消息)
- IDSender(代表发送消息的用户ID)
- IDReceiver(代表接收消息的用户ID)
- SenderName(代表发送消息的用户名)
- ReceiverName(表示接收消息的用户名)
- 消息(表示会话中发送的消息)
- DateTime(表示发送消息的日期时间,格式为yyyy-mmm-dd)
在我的 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) 的代码有问题,因为一旦我启动应用程序,只有一个单元格包含最后一个对话和最后一条消息,但不会出现与其他相关帖子的对话.
请帮帮我!
【问题讨论】:
标签: ios objective-c uitableview