学习笔记-斯坦福iOS7-第十三课:Core Data 和表格视图

一. Core Data and UITableView
1. NSFetchedResultsController
a. 把NSFetchRequest 和 UITableViewController 关联起来;
b. 在UITableViewController 中需要有个NSFetchedResultsController 属性;
c. 例如:
- (NSUInteger)numberOfSectionsInTableView:(UITableView*)sender {
    return [[self.fetchedResultsController sections] count];
}

- (NSUInteger)tableView:(UITableView*)sender numberOfRowsInSection:(NSUInteger)section {
    return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}

- (UITableViewCell*)tableView:(UITableView*)sender
         cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    UITableViewCell *cell = ...;
    Photo *photo = (Photo*)[self.fetchedResultsController objectAtIndexPath:indexPath];
    // ...
    // ...
    return cell;



d. 创建 NSFetchedResultsController
. 需要NSFetchRequest 和 NSManagedObjectContext
. code :
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] 
              initWithFetchRequest:(NSFetchRequest*)request
     managedObjectContext:(NSManagedObjectContext*)context
                sectionNameKeyPath:(NSString*)keyThatSaysWhichSectionEachManagedObjectIsIn
                         cacheName:@"MyPhotoCache"];


e. delegate : Core Data 发送变化时,调用,更新TableVIew
- (void)controller:(NSFetchedResultsController*)controller
   didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath*)indexPath
     forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath*)newIndexPath {
}


2. Demo 比较完整



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值