//  YJYMasterViewController.m

#import "YJYMasterViewController.h"

#import "YJYDetailViewController.h"

/*

@interface:接口,提供类的公共描述,接口里面包含了使用该类的所需信息,编译此部分后,就能够使用该的对象及调用类方法。

@implementation:实现,告诉编译器如何让该类工作,实现了接口中声明的方法。

代码分为接口和实现两部分,

接口部分包含:@interface指令、公共struct定义、enum常量、@defines和extern全局变量等。

实现部分包含:@implementation指令、全局变量的定义、私有struct等。

#import:导入头文件:头文件包含元素声明(如,结构体、符号常量、函数原型等),#import同c语言中的#include类似,它们的区别在于,在c语言中,通常使用#ifdef命令来避免一个头文件包含另一个文件,而#import可保证头文件只被包含一次。

带尖括号语句用来导入系统头文件(只读),带引号的语句用来导入项目本地头文件(可读)。

*/

@interface YJYMasterViewController ()

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;

@end

@implementation YJYMasterViewController

/*

当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的 awakeFromNib函数来响应这个消息,

执行一些必要的操作。也就是说通过nib文件创建view对象是执行awakeFromNib

*/

- (void)awakeFromNib

{

self.clearsSelectionOnViewWillAppear = NO;

self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);

[superawakeFromNib];

}

//当view对象被加载到内存是就会执行viewDidLoad,所以不管通过nib文件还是代码的方式创建对象都会执行viewDidLoad

- (void)viewDidLoad

{//释放视图资源

NSLog(@"viewDidLoad() begin...");

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.navigationItem.leftBarButtonItem = self.editButtonItem;

UIBarButtonItem *addButton = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:@selector(insertNewObject:)];

self.navigationItem.rightBarButtonItem = addButton;

self.detailViewController = (YJYDetailViewController *)[[self.splitViewController.viewControllerslastObject] topViewController];

NSLog(@"viewDidLoad() over...");

}

- (void)didReceiveMemoryWarning

{

NSLog(@"didReceiveMemoryWarning() begin...over");

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (void)insertNewObject:(id)sender

{

NSLog(@"insertNewObject() begin...");

NSManagedObjectContext *context = [self.fetchedResultsControllermanagedObjectContext];

NSEntityDescription *entity = [[self.fetchedResultsControllerfetchRequest] entity];

NSManagedObject *newManagedObject = [NSEntityDescriptioninsertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

// If appropriate, configure the new managed object.

// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.

[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];

// Save the context.

NSError *error = nil;

if (![context save:&error]) {

// Replace this implementation with code to handle the error appropriately.

// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

abort();

}

NSLog(@"insertNewObject() over...");

}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

NSLog(@"numberOfSectionsInTableView being...over");

return [[self.fetchedResultsControllersections] count];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

NSLog(@"tableView numberOfRowsInSection() beging...");

id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];

NSLog(@"tableView numberOfRowsInSection() over...");

return [sectionInfo numberOfObjects];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

NSLog(@"tableView cellForRowAtIndexPath() beging...");

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"forIndexPath:indexPath];

[self configureCell:cell atIndexPath:indexPath];

NSLog(@"tableView cellForRowAtIndexPath() over...");

return cell;

}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{//该方法控制master区的可编辑(删除)操作

// Return NO if you do not want the specified item to be editable.

return YES;

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

if (editingStyle == UITableViewCellEditingStyleDelete) {

NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];

[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

NSError *error = nil;

if (![context save:&error]) {

// Replace this implementation with code to handle the error appropriately.

// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

abort();

}

}

}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

// The table view should not be re-orderable.

returnYES;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];

self.detailViewController.detailItem = object;

}

#pragma mark - Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController

{

if (_fetchedResultsController != nil) {

return_fetchedResultsController;

}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

// Edit the entity name as appropriate.

NSEntityDescription *entity = [NSEntityDescriptionentityForName:@"Event"inManagedObjectContext:self.managedObjectContext];

[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.

[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];

NSArray *sortDescriptors = @[sortDescriptor];

[fetchRequest setSortDescriptors:sortDescriptors];

// Edit the section name key path and cache name if appropriate.

// nil for section name key path means "no sections".

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsControlleralloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContextsectionNameKeyPath:nilcacheName:@"Master"];

aFetchedResultsController.delegate = self;

self.fetchedResultsController = aFetchedResultsController;

NSError *error = nil;

if (![self.fetchedResultsControllerperformFetch:&error]) {

// Replace this implementation with code to handle the error appropriately.

// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

abort();

}

return_fetchedResultsController;

}

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller

{

[self.tableViewbeginUpdates];

}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo

atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type

{

switch(type) {

caseNSFetchedResultsChangeInsert:

[self.tableViewinsertSections:[NSIndexSetindexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];

break;

caseNSFetchedResultsChangeDelete:

[self.tableViewdeleteSections:[NSIndexSetindexSetWithIndex: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) {

caseNSFetchedResultsChangeInsert:

[tableView insertRowsAtIndexPaths:@[newIndexPath]withRowAnimation:UITableViewRowAnimationFade];

break;

caseNSFetchedResultsChangeDelete:

[tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];

break;

caseNSFetchedResultsChangeUpdate:

[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];

break;

caseNSFetchedResultsChangeMove:

[tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];

[tableView insertRowsAtIndexPaths:@[newIndexPath]withRowAnimation:UITableViewRowAnimationFade];

break;

}

}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller

{

[self.tableViewendUpdates];

}

/*

// Implementing the above methods to update the table view in response to individual changes may have performance implications if a large number of changes are made simultaneously. If this proves to be an issue, you can instead just implement controllerDidChangeContent: which notifies the delegate that all section and object changes have been processed.

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller

{

// In the simplest, most efficient, case, reload the table view.

[self.tableView reloadData];

}

*/

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath

{

NSLog(@"configureCell atIndexPath() begin...");

NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];

cell.textLabel.text = [[object valueForKey:@"timeStamp"] description];

NSLog(@"configureCell atIndexPath() over...");

}

@end

转载于:https://www.cnblogs.com/allearner/p/3623110.html

Note_Master-Detail Application(iOS template)_05_ YJYMasterViewController.m相关推荐

  1. beta应用程序ios_通过构建简单的食谱应用程序来了解iOS最佳做法

    beta应用程序ios by Khoa Pham 通过Khoa Pham 通过构建简单的食谱应用程序来了解iOS最佳做法 (Learn iOS best practices by building a ...

  2. iOS 架构模式 - 简述 MVC, MVP, MVVM

    Make everything as simple as possible, but not simpler - Albert Einstein,把每件事,做简单到极致,但又不过于简单 - 阿尔伯特· ...

  3. pages文件夹 AddCartSuccess Center Detail Home Login Pay PaySuccess

    pages文件夹 AddCartSuccess <template><div class="cart-complete-wrap"><div clas ...

  4. ios swiftui_ios swiftui中的本地化

    ios swiftui 本土化 (Localisation) Localisation is the process of making your app support another langua ...

  5. iOS系列教程之常见开发Tips(转自阿峰的博客)

    开发技巧和常见错误汇总,不定期更新中,也欢迎大家总结跟帖 1:ARC下NSNotificationCenter需要remove - (void) dealloc { // [super dealloc ...

  6. android 高仿ios开关,Android自定义view仿IOS开关效果

    本文主要讲解如何在 Android 下实现高仿 iOS 的开关按钮,并非是在 Android 自带的 ToggleButton 上修改,而是使用 API 提供的 onDraw.onMeasure.Ca ...

  7. 20.进入商品详情页Detail

    like.vue组件中 给li添加一个点击事件 @click="goDetail" 对应的方法 methods:{goDetail(){this.$router.push({pat ...

  8. 【前端】Vue项目:旅游App-(22)detail:房屋信息、房屋设施、插槽

    文章目录 目标 过程与代码 房屋信息部分info 相似结构的组件section 房屋设施facility 效果 总代码 修改或添加的文件 detail-section detail-facility ...

  9. XCode 4.2.1 项目的几个模版说明

    XCode 4.2.1 项目的模版截图: Single View Application This template provides a starting point for an applicat ...

  10. IPhone 编程 XCode4.2 项目模板说明

    2019独角兽企业重金招聘Python工程师标准>>> 入手了台Mac Mini 2010版,只有可怜的2G内存,由于经济问题,不敢随便升级到8G内存,只是升级到10.6.8,xco ...

最新文章

  1. Iframe 用法浅析
  2. 图灵奖得主门徒、RISC-V 创始成员领衔,睿思芯科获数千万美金融资 | AI 创业周报第6期...
  3. Java多线程之Lock接口
  4. Controller 如果能保持单例,尽量使用单例
  5. Android 快速开发框架Afinal
  6. 字符串的排序不一定要完全按照字典序(洛谷P1012题题解,Java语言描述)
  7. 蓝桥杯 ALGO-52 算法训练 排列问题
  8. 多时隙编码aloha(MuSCA)与编码时隙aloha(CSA)协议分析
  9. 汇编心得(一)在32位机上实现64位数的相加
  10. lvds输入悬空_LVDS的接口电路设计
  11. 富斯FS_I6六通道遥控器玩转APM飞控失控保护
  12. SGD和带momentum的SGD算法
  13. c++学习书籍推荐《深度探索C++对象模型》下载
  14. java编程培训都学习哪些内容
  15. 《统计学基于R》第一章 数据与R
  16. 全排列回溯算法C语言,回溯算法(以全排列为例)
  17. 异次元发卡系统源码荔枝发卡V3.0
  18. 机器学习-样本不均衡现象
  19. 利用OpenCv程序生成棋盘格标定板,并且实现像素单位与米制单位的转换
  20. 优化命令----sar命令

热门文章

  1. 银行卡所属银行的查询接口--阿里提供
  2. 网站安全防护方案--WEB应用防火墙
  3. [CodeForces] 274E Mirror Room
  4. HTML5简单实战--休假申请单
  5. srm32定时器的ETR功能
  6. 游戏服务器经常被DDOS攻击应该怎么办?
  7. html标签-meta
  8. in作为介词的用法_英语介词丨in、on 、at 的具体用法到底有哪些?
  9. c 语言万年历 节日查询,C语言 程序设计 节日查询和任务提醒系统万年历.doc
  10. 缺省值及属性(Python)