您的位置:首页 > 其它

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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  FRC TableView