在请求旧数据时,使用fetchedResultsController委托在Top而不是bottom显示tableview的旧消息

时间:2015-01-14 18:41:44

标签: ios uitableview nsfetchedresultscontroller nsfetchrequest

目前,我在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看到

Whe I load more Data it shows the Messages in the right Order from new to older but the issue is that I want to show newer messages Below and Older messages Above.

当我加载更多数据时,它会以正确的顺序显示从新到旧的消息,但问题是我想在下面显示更新的消息以及上面的旧消息。

1 个答案:

答案 0 :(得分:0)

让您的获取请求控制器将ASCENDING排序为YES

  

[self.fetchedResultsController.fetchRequest   setSortDescriptors:@ [NSSortDescriptor   sortDescriptorWithKey:@"&DATE_MESSAGE#34;升序:YES]]];