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)的代碼時出現錯誤,因爲一旦我啓動應用程序,只有一個單元格包含最後一次對話和最後一條消息,但不會與其他相關文章進行對話。
請幫幫我!
由於盧卡!!!! – Pinturikkio