2013-05-27 62 views
0

我正在製作聊天應用程序,希望檢索表格單元格/行中的最新消息。如果消息少於100則我可以能夠在表格視圖中檢索最新消息,但是當消息數量超過100時,則無法檢索最新消息。如何在uitableview中加載/顯示超過100個單元格(行)或在uitableview中檢索最新消息

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return [chatData count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
chatCell *cell = (chatCell *)[tableView  dequeueReusableCellWithIdentifier:CHAT_CELL_IDENTIFIER]; 
NSUInteger row = indexPath.row; 
if (row < chatData.count) 
{ 
    NSString *chatText = [[chatData objectAtIndex:row] objectForKey:TEXT]; 
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; 
    UIFont *font = [UIFont systemFontOfSize:14]; 
    CGSize size = [chatText sizeWithFont:font constrainedToSize:CGSizeMake(225.0f, 1000.0f) lineBreakMode:NSLineBreakByCharWrapping]; 
    cell.textString.frame = CGRectMake(75, 14, size.width +20, size.height + 20); // set text frame 
    cell.textString.font = [UIFont fontWithName:FONT_NAME size:FONT_SIZE];  // set text font 
    cell.textString.text = chatText;            // set text 
    [cell.textString sizeToFit]; 
    NSDate *theDate = [[chatData objectAtIndex:row] objectForKey:DATE]; 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:DATE_FORMAT]; 
    NSString *timeString = [formatter stringFromDate:theDate]; 
    cell.timeLabel.text = timeString;          // set timeLabel to display date and time 
    cell.userLabel.text = [[chatData objectAtIndex:row] objectForKey:NAME]; // set userLabel to display userName 
} 
return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *cellText = [[chatData objectAtIndex:indexPath.row] objectForKey:TEXT]; 
UIFont *cellFont = [UIFont fontWithName:FONT_NAME size:FONT_SIZE]; 
CGSize constraintSize = CGSizeMake(225.0f, MAXFLOAT); 
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping]; 
return labelSize.height + 40; 
} 




- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
[textField resignFirstResponder]; 

if (tfEntry.text.length>0) 
{ 
    // updating the table immediately 
    NSArray *keys = [NSArray arrayWithObjects:TEXT,SET_SENDER ,SET_RECEIVER ,DATE ,nil]; 
    NSArray *objects = [NSArray arrayWithObjects:tfEntry.text, Sender, receiver, [NSDate date], nil]; 
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 
    [chatData addObject:dictionary]; 

    NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init]; 
    NSIndexPath *newPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [insertIndexPaths addObject:newPath]; 
    [chatTable beginUpdates]; 
    [chatTable insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationBottom]; 
    [chatTable endUpdates]; 
    [chatTable reloadData]; 

    // going for the parsing 
    PFObject *newMessage = [PFObject objectWithClassName:CHATROOM]; 
    [newMessage setObject:tfEntry.text forKey:TEXT]; 
    [newMessage setObject:self.userId forKey:SET_SENDER]; 
    [newMessage setObject:receiver forKey:SET_RECEIVER]; 
    [newMessage setObject:[NSDate date] forKey:DATE]; 
    [newMessage setObject:self.user.userName forKey:NAME]; 
    [newMessage saveInBackground]; 
    localChatCount = [chatData count]; 
    newChatMessage = NEW_CHAT_MESSAGE; 
    tfEntry.text = @""; 
} 
return NO; 
} 

那麼如果行/單元數大於100,檢索tableview中的最新消息的過程是什麼?任何幫助將不勝感激。

+0

你能顯示一些源代碼嗎? –

+1

是的,我添加了源代碼。 – Ponting

+0

你的問題到底是什麼?我真的不明白這些單元格發生了什麼> 100?你不能創建它們嗎?他們不出現嗎?你不能再向下滾動嗎?是什麼讓你相信100是問題出現的門檻? –

回答

0

我刪除了行,如果錶行數超過100,並從解析表中檢索最近的100行。所以不需要超過100行。

如果錶行數超過100,我也可以爲PFQuery對象設置限制和跳過值。

相關問題