what i am trying to do to display only 10 item inside UITableView From Core Data that was easy using [fetchRequest setFetchLimit:10]; but after insertion of the 11th item the first element should not longer be visible inside UITableView and the total number of element should be always 10 i already made many researches about this topic i just found only Get Selected index of UITableView so what i need to to get the index of the row to check wether is larger then 10 delete the 9th element here is the full code needed:
#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;
}
}