在上一篇博客中,咱們討論了CoreData框架中添加與查詢數據的操做,事實上,在大多數狀況下,這些數據都是由一個UITableView表視圖進行展現的,所以,CoreData框架中還未開發者提供了一個類NSFetchedResultsController,這個類做爲橋接,將視圖與數據進行綁定。sql
添加與查詢數據操做:http://my.oschina.net/u/2340880/blog/611430。框架
NSFetchedResultsController的初始化須要一個查詢請求和一個數據操做上下文。代碼示例以下:fetch
//遵照協議 @interface ViewController ()<NSFetchedResultsControllerDelegate> { //數據橋接對象 NSFetchedResultsController * _fecCon; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //進行初始化操做 NSURL *modelUrl = [[NSBundle mainBundle]URLForResource:@"Model" withExtension:@"momd"]; NSManagedObjectModel * mom = [[NSManagedObjectModel alloc]initWithContentsOfURL:modelUrl]; NSPersistentStoreCoordinator * psc = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:mom]; NSURL * path =[NSURL fileURLWithPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"CoreDataExample.sqlite"]]; [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:path options:nil error:nil]; NSManagedObjectContext * moc = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType]; [moc setPersistentStoreCoordinator:psc]; NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"SchoolClass"]; //設置數據排序 [request setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"stuNum" ascending:YES]]]; //進行數據橋接對象的初始化 _fecCon = [[NSFetchedResultsController alloc]initWithFetchRequest:request managedObjectContext:moc sectionNameKeyPath:nil cacheName:nil]; //設置代理 _fecCon.delegate=self; //進行數據查詢 [_fecCon performFetch:nil]; } @end
用於初始化NSFecthedResultsController的數據請求對象必須設置一個排序規則。在initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:方法中,若是設置第三個參數,則會以第三個參數爲鍵值進行數據的分區。當數據發生變化時,將經過代理進行方法的回調。spa
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellid"]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellid"]; } //獲取相應數據模型 SchoolClass * obj = [_fecCon objectAtIndexPath:indexPath]; cell.textLabel.text = obj.name; cell.detailTextLabel.text = [NSString stringWithFormat:@"有%@人",obj.stuNum]; return cell; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [_fecCon sections].count; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ id<NSFetchedResultsSectionInfo> info = [_fecCon sections][section]; return [info numberOfObjects]; }
效果以下:.net
//數據將要改變時調用的方法 - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { //開啓tableView更新預處理 [[self tableView] beginUpdates]; } //分區數據改變時調用的方法 - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)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; //移動分區 case NSFetchedResultsChangeMove: //更新分區 case NSFetchedResultsChangeUpdate: break; } } //數據改變時回調的代理 - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { switch(type) { //插入數據 case NSFetchedResultsChangeInsert: [[self tableView] insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; //刪除數據 case NSFetchedResultsChangeDelete: [[self tableView] deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; //更新數據 case NSFetchedResultsChangeUpdate: [self reloadData]; break; //移動數據 case NSFetchedResultsChangeMove: [[self tableView] deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [[self tableView] insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; } } //數據更新結束調用的代理 - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [[self tableView] endUpdates]; }
專一技術,熱愛生活,交流技術,也作朋友。代理
——琿少 QQ羣:203317592code