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];
}


转载于:https://my.oschina.net/AChang/blog/552224

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值