zoukankan      html  css  js  c++  java
  • iOS_SN_CoreData(二)

    上一篇是讲怎么进行CoreData的基本操作,这一篇是讲NSFetchedResultsController得到CoreData数据怎么与tableView完美结合,和动画操作的实现。

    NSFetchedResultsController的结果与tableView的结合

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return [[self.queryData sections] count];
    }
    
    - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
        if ([[self.queryData sections] count] > 0) {
            id <NSFetchedResultsSectionInfo> sectionInfo = [[self.queryData sections] objectAtIndex:section];
            return [sectionInfo numberOfObjects];
        } else
            return 0;
    }
    

      

    NSFetchedResultsControllerDelegate代理方法的实现

    CoreData中数据将要改变调用的方法

    - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
        
        // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
        [self.tableView beginUpdates];
    }
    

    CoreData中行数据改变调用的方法可以在数据改变时实现某些动画

    - (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:@[newIndexPath] withRowAnimation:UITableViewRowAnimationRight];
                break;
                
            case NSFetchedResultsChangeDelete:
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
                break;
                
            case NSFetchedResultsChangeUpdate:
                [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
                break;
                
            case NSFetchedResultsChangeMove:
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                break;
        }
    }
    

    CoreData中组的数据改变时调用的方法也可以实现某些动画

    - (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:UITableViewRowAnimationAutomatic];
                break;
            case NSFetchedResultsChangeMove:
                break;
            case NSFetchedResultsChangeUpdate:
                break;
            case NSFetchedResultsChangeDelete:
                [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
                break;
        }
    }
    

    CoreData中的数据已经改变时调用的方法

    - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
        self.dataList = self.queryData.fetchedObjects.mutableCopy;
        NSLog(@"%@",self.dataList);
        [self.tableView endUpdates];
    }
    

      

    本文GitHub地址https://github.com/zhangkiwi/iOS_SN_CoreData

  • 相关阅读:
    CHtmlEditCtrl (3): More HTML Editor Options
    CHtmlEditCtrl (2): Add a Source Text Editor to Your HTML Editor
    CHtmlEditCtrl(1) : Use CHtmlEditCtrl to Create a Simple HTML Editor
    HttpWebRequest抓取网页数据返回异常:远程服务器返回错误: (503) 服务器不可用
    HttpWebRequest: Remote server returns error 503 Server Unavailable
    所有的图片后缀名
    system函数的应用一例
    esUtil.h中的m变量报错
    IIS通过HTML5实现应用程序缓存的离线浏览
    鼠标悬浮在img上让图片变大
  • 原文地址:https://www.cnblogs.com/zhang-kiwi/p/5244853.html
Copyright © 2011-2022 走看看