0

我正在制作想要在表格单元格/行中检索最新消息的聊天应用程序。如果消息少于 100 条,那么我可以在表格视图中检索最新消息,但是当消息计数超过 100 条时,它无法检索最新消息.

- (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 中检索最新消息的过程是什么?任何帮助将不胜感激。

4

1 回答 1

0

如果表行超过 100 行,我删除行并从解析表中检索最新的 100 行。所以不需要超过 100 行。

如果表行超过 100,我还可以为 PFQuery 对象设置限制和跳过值。

于 2013-06-06T10:43:18.933 回答