【发布时间】:2015-08-11 07:20:56
【问题描述】:
我想要在 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;
}
}
【问题讨论】:
标签: ios objective-c uitableview core-data