FRC与tableview的联合使用
2015-12-28 00:00
501 查看
摘要: 通过FRC查询本地数据,返回到Tableview界面作为数据源显示。
#import "CollectTableViewController.h" #import "VidioEntity.h" #import "VidioManager.h" #import "UITableViewCell+Func.h" #import "MoviePlayerViewController.h" @interface CollectTableViewController () @property (nonatomic, strong) VidioManager * vidioManager; @property (nonatomic, strong) NSManagedObjectContext * managedObjectContext; @property (nonatomic,strong)NSFetchedResultsController * fetchedResultsController; @property (nonatomic, strong) NSArray * collectArray; @end @implementation CollectTableViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableView .rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 200; self.vidioManager = [VidioManager share]; //配置FRC UIApplication * app = [UIApplication sharedApplication]; id delegate = [app delegate]; self.managedObjectContext = [delegate managedObjectContext]; //设置请求 NSFetchRequest * requrest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([VidioEntity class])]; NSSortDescriptor * sort = [NSSortDescriptor sortDescriptorWithKey:@"dateString" ascending:NO]; [requrest setSortDescriptors:@[sort]]; self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:requrest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"uid" cacheName:nil]; self.fetchedResultsController.delegate = self; NSError * error; if (![self.fetchedResultsController performFetch:&error]) { NSLog(@"error = %@",error.localizedDescription); } _collectArray = [self.fetchedResultsController sections]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return _collectArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { id<NSFetchedResultsSectionInfo>sectionInfo = _collectArray[section]; return [sectionInfo numberOfObjects]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //用实体接收查到的数据 VidioEntity * vidio = [self.fetchedResultsController objectAtIndexPath:indexPath]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CollectCell" forIndexPath:indexPath]; [cell setCellInfo:vidio]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath { // UIStoryboard * collect = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; // UIViewController * viewController = [collect instantiateViewControllerWithIdentifier:@"PlayVidioViewController"]; // [self setHidesBottomBarWhenPushed:YES]; // [self.navigationController pushViewController:viewController animated:YES]; // [self setHidesBottomBarWhenPushed:NO]; VidioEntity * vidio = [self.fetchedResultsController objectAtIndexPath:indexPath]; MoviePlayerViewController * movie = [[MoviePlayerViewController alloc]init]; [movie setValue:vidio forKey:@"vidioEntity"]; [self presentViewController:movie animated:YES completion:^{ }]; // [self.navigationController pushViewController:movie animated:YES]; } //侧滑删除 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //删除数据 VidioEntity * vidio = [self.fetchedResultsController objectAtIndexPath:indexPath]; [self.managedObjectContext deleteObject:vidio]; NSError * error; if (![self.managedObjectContext save:&error]) { NSLog(@"%@ %s",error.localizedDescription,__func__); } } } #pragma frc回调机制 - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [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; } } - (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: [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeMove: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; } } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; }
相关文章推荐
- JavaFX之TableView的使用详解
- tableView上面空出20个像素的解决办法
- IOS 学习笔记 —— tableView 使用详解(二)
- TableView
- 报错-Type ViewController doesnot conform to protocol
- TableView的基本使用
- 根据首字母查找单词
- 下拉放大tableView头视图
- 下拉 放大tableview上方 图片
- TableView详细解释
- tableView的上拉刷新和下拉刷新
- 开发学习笔记--UItableView
- TableView多汽车数据,模型嵌套
- 解决tableview出现多余行
- UITableView的用法详解
- IOS 解决tableView header 跟着滑的问题
- tableView的滑动删除
- 使用autoLayout布局ScrollView(内嵌多个TableView)的一些小结
- /SourceCache/UIKit_Sim/UIKit-3347.44/UITableView.m:1623崩溃的原因
- TableView界面传值