获取将在UITableView中插入的行的索引

时间:2015-08-11 07:20:57

标签: ios objective-c uitableview core-data

我想要做的只是在 UITableView 中显示10个项目来自核心数据,这很容易使用 [fetchRequest setFetchLimit:10]; 但是在插入第11个之后item第一个元素不应该在 UITableView 中可见,并且元素的总数应该总是10我已经对这个主题进行了很多研究我只发现了Get Selected index of UITableView所以我需要什么获取要检查的行的索引是否大于10删除第9个元素这里是需要的完整代码:

#import "ReservationHistoryTableViewController.h"
#import "CoreData.h"
#import "ReservationEntity.h"
#import "EntryCell.h"
#import "DetailedHistoryViewController.h"
@interface ReservationHistoryTableViewController () <NSFetchedResultsControllerDelegate>


@property (strong, nonatomic) NSFetchedResultsController *fetchedResultController ;

@end

@implementation ReservationHistoryTableViewController

- (IBAction)refresh:(id)sender {

    [self getAllReservationHistory];
    [sender endRefreshing];

}






- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier =  @"Cell";
    EntryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    ReservationEntity *reservationEntry  = [self.fetchedResultController objectAtIndexPath:indexPath];
    [cell configureCellForEntry:reservationEntry];

    return cell;
}







-(NSFetchRequest *) entryListFetchRequest {
    NSFetchRequest *fetchRequest  = [NSFetchRequest fetchRequestWithEntityName:@"Reservations"];
    [fetchRequest setFetchLimit:10];
    [self.tableView reloadData];
    fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"reservationID" ascending:NO]];
    return fetchRequest;

}


// this method is used to fetch the data //
-(NSFetchedResultsController *) fetchedResultController {
    if(_fetchedResultController != nil)
        return _fetchedResultController;
    CoreData *coreDataStack = [CoreData defaultStack];
    NSFetchRequest *fechtRequest = [self entryListFetchRequest];
    _fetchedResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:fechtRequest managedObjectContext:coreDataStack.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    _fetchedResultController.delegate = self;
    return _fetchedResultController;

}




-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"details"])
    {
        UITableViewCell *cell = sender;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        UINavigationController *naviagationController = segue.destinationViewController;
        DetailedHistoryViewController *detailedHisotryViewController = (DetailedHistoryViewController *) naviagationController.topViewController;
        detailedHisotryViewController.entry = [self.fetchedResultController objectAtIndexPath:indexPath];
    }
}




-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
    switch (type) {
        case NSFetchedResultsChangeInsert:

//            NSIndexPath *selectedIndexPath = [self. indexPathForSelectedRow];



            if(newIndexPath.section > 10)
            {
                [self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                [self.tableView deleteRowsAtIndexPaths:@[@(9)] withRowAnimation:UITableViewRowAnimationAutomatic];
// even i try if(newIndexPath.row) i could not reach my target // 

            }


            else
            {
                [self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            }

            break;
        default:
            break;
    }
}

1 个答案:

答案 0 :(得分:2)

你的方向错了。 NSIndexPath是识别行和部分的人,但您使用的是CoreData,并且您不应该自己使这种逻辑复杂化。

正确的解决方案是根据Apple指南实施NSFetchedResultsController(您可以复制并粘贴):

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
    [self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

    UITableView *tableView = self.tableView;

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
           [tableView deleteRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
           [tableView insertRowsAtIndexPaths:[NSArray
arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
           break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id )sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
    [self.tableView endUpdates];
}

删除你应该做的对象:

[coreDataStack.managedObjectContext deleteObject:object];

用于插入对象:

[coreDataStack.managedObjectContext insertObject:object];

您也可以只插入对象 - 如果它属于NSFetchedResultsController NSFetchRequest条款 - 它将自动为您重新获取,您的视图将通过我上面粘贴的委托方法获得通知。