前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS中CoreData数据管理系列四——进行数据与页面的绑定

iOS中CoreData数据管理系列四——进行数据与页面的绑定

作者头像
珲少
发布2018-08-15 17:07:42
6820
发布2018-08-15 17:07:42
举报
文章被收录于专栏:一“技”之长一“技”之长

iOS中CoreData数据管理系列四——进行数据与页面的绑定

一、引言

    在上一篇博客中,我们讨论了CoreData框架中添加与查询数据的操作,事实上,在大多数情况下,这些数据都是由一个UITableView表视图进行展示的,因此,CoreData框架中还未开发者提供了一个类NSFetchedResultsController,这个类作为桥接,将视图与数据进行绑定。

添加与查询数据操作:http://my.oschina.net/u/2340880/blog/611430

二、进行数据初始化

    NSFetchedResultsController的初始化需要一个查询请求和一个数据操作上下文。代码示例如下:

代码语言:javascript
复制
//遵守协议
@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:方法中,如果设置第三个参数,则会以第三个参数为键值进行数据的分区。当数据发生变化时,将通过代理进行方法的回调。

三、与UITableView进行数据绑定 

代码语言:javascript
复制
-(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];
    
}

效果如下:

四、将数据变化映射到视图

代码语言:javascript
复制
//数据将要改变时调用的方法
- (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];
}

专注技术,热爱生活,交流技术,也做朋友。 ——珲少

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016/02/01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • iOS中CoreData数据管理系列四——进行数据与页面的绑定
    • 一、引言
      • 二、进行数据初始化
        • 三、与UITableView进行数据绑定 
          • 四、将数据变化映射到视图
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档