我只是想问问我应该使用什么,并能够做我想做的事情。 例如,从当前的firebase值中提取出来的值变成Json,我可以得到以下信息: 通过此代码: 这是可行的,但是我想做的是在数据库得到更新时。 像图片中一样的新输入将位于其底部的最后一个输入的顶部 例如: ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
目前,我有一个Tableview,它在加载时向Core-Data发送请求,并获取较新消息的前10条消息。
// Initialize Fetch Request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Chat"];
// Add Sort Descriptors
fetchRequest.fetchLimit = 10;
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"room = %@", _roomid];
[fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"date_message" ascending:NO]]]; // YES;
// Initialize Fetched Results Controller
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
// Perform Fetch
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];
if (error) {
NSLog(@"Unable to perform fetch.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
在我打电话给他们之后。 消息正确显示在下面。 但是,当我加载(按需获取较旧的消息)时,会使用以下内容。
- (void)fetchOldMessages {
[self.fetchedResultsController.fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"date_message" ascending:NO]]];
_fetchedResultsController.fetchRequest.fetchLimit +=10;
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];
if (error) {
NSLog(@"Unable to perform fetch.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
[self.tableView reloadData];
}
问题是旧消息现在显示在下面而不是新消息的上面。 新插入的消息也显示在tableView的顶部而不是底部。
这是我的IndexPath:
- (UITableViewCell*)tableView:(UITableView*)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Chat *messageInfo = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSString *CellIdentifier = @"fromMe";
FromMeTableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
cell.message_me.clipsToBounds = YES;
cell.message_me.layer.cornerRadius = 15;
cell.message_me.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 8.0, 8.0);
cell.message_me.text = messageInfo.message;
cell.message_date_me.text = [NSString stringWithFormat:@"%@", messageInfo.date_message];
cell.avatar_message_me.clipsToBounds = YES;
cell.avatar_message_me.layer.cornerRadius = cell.avatar_message_me.frame.size.width / 2;
cell.avatar_message_me.image = user_images[@"me"];
cell.message_me.backgroundColor = [UIColor colorWithRed:0.098 green:0.737 blue:0.611 alpha:1];
cell.message_me.textColor = [UIColor whiteColor]; //[UIColor whiteColor];
// Date Settings
NSDate *today = [[NSDate alloc]init];
NSDate *message_date = messageInfo.date_message;
NSDateFormatter *formatDate = [[NSDateFormatter alloc]init];
[formatDate setDateFormat:@"yyyy-MM-dd"];
NSString *today_string = [formatDate stringFromDate:today];
NSString *message_date_string = [formatDate stringFromDate:message_date];
// Make Date validation case is older display full date
if([today_string isEqualToString:message_date_string]){
NSDateFormatter *timeformat = [[NSDateFormatter alloc] init];
[timeformat setDateFormat:@"hh:mm a"];
NSDate *message_date = messageInfo.date_message;
NSString *string_date = [timeformat stringFromDate:message_date];
cell.message_date_me.text = [NSString stringWithFormat:@"Today at %@",[NSString stringWithFormat:@"%@",string_date]];
}
else {
NSDateFormatter *timeformat = [[NSDateFormatter alloc] init];
[timeformat setDateFormat:@"EEEE d hh:mm a"];
NSDate *message_date = messageInfo.date_message;
NSString *string_date = [timeformat stringFromDate:message_date];
cell.message_date_me.text = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",string_date]];
}
return cell;
}
这是第一次加载时显示5条消息,但最后一条消息应该关闭,而不是如DATE所示
当我加载更多数据时,它以从新到旧的正确顺序显示消息,但问题是我想在下面显示较新的消息,在上面显示较旧的消息。
使您的提取请求控制器将ASCENDING排序为YES
[self.fetchedResultsController.fetchRequest setSortDescriptors:@ [[NSSortDescriptor sortDescriptorWithKey:@“ date_message”升序:是]]]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.