当前位置: 首页 > news >正文

FRC与tableview的联合使用

为什么80%的码农都做不了架构师?>>>   hot3.png

#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

相关文章:

  • Sublime Text 3安装与使用
  • 解放全天下开发者
  • Cesium 获取当前视图范围
  • Gradle介绍
  • 扩展SpringMVC以支持绑定JSON格式的请求参数
  • 0x05 Python数据分析,Anaconda八斩刀
  • dos保存adb logcat读取的Android信息
  • Rotate Image LeetCode
  • MFC消息映射与命令传递
  • CentOS6.6下解压安装mysql-5.7.10-linux-glibc2.5-i686.tar.gz
  • myeclipse安装插件phpeclipse后进行PHP代码编写
  • 怎样选择前端框架
  • 【遇到的问题】父div不能被撑开
  • iOS开发-UIView扩展CGRect
  • 如何使用 SPICE client (virt-viewer) 来连接远程虚拟机桌面?
  • .pyc 想到的一些问题
  • [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示
  • 【347天】每日项目总结系列085(2018.01.18)
  • 【EOS】Cleos基础
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • Angular 2 DI - IoC DI - 1
  • Docker入门(二) - Dockerfile
  • fetch 从初识到应用
  • HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
  • iOS编译提示和导航提示
  • PAT A1017 优先队列
  • Quartz实现数据同步 | 从0开始构建SpringCloud微服务(3)
  • SpringCloud集成分布式事务LCN (一)
  • uva 10370 Above Average
  • vue-loader 源码解析系列之 selector
  • XForms - 更强大的Form
  • 解析 Webpack中import、require、按需加载的执行过程
  • 漂亮刷新控件-iOS
  • 如何设计一个微型分布式架构?
  • 如何使用 JavaScript 解析 URL
  • 我看到的前端
  • 学习ES6 变量的解构赋值
  • PostgreSQL 快速给指定表每个字段创建索引 - 1
  • 函数计算新功能-----支持C#函数
  • ​Kaggle X光肺炎检测比赛第二名方案解析 | CVPR 2020 Workshop
  • ​一些不规范的GTID使用场景
  • #《AI中文版》V3 第 1 章 概述
  • #DBA杂记1
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (02)vite环境变量配置
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (arch)linux 转换文件编码格式
  • (C语言)输入自定义个数的整数,打印出最大值和最小值
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100
  • (推荐)叮当——中文语音对话机器人
  • (未解决)jmeter报错之“请在微信客户端打开链接”
  • (一)Java算法:二分查找
  • (转载)hibernate缓存
  • .describe() python_Python-Win32com-Excel