Note_Master-Detail Application(iOS template)_05_ YJYMasterViewController.m
// 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
Note_Master-Detail Application(iOS template)_05_ YJYMasterViewController.m的更多相关文章
- Note_Master-Detail Application(iOS template)_04_ YJYMasterViewController.h
//YJYMasterViewController.h #import <UIKit/UIKit.h> @classYJYDetailViewController; #import < ...
- Note_Master-Detail Application(iOS template)_02_YJYAppDelegate.m
//YJYAppDelegate.m #import "YJYAppDelegate.h" #import "YJYMasterViewController.h" ...
- Note_Master-Detail Application(iOS template)_07_ YJYDetailViewController.m
// YJYDetailViewController.m #import "YJYDetailViewController.h" @interfaceYJYDetailViewC ...
- Note_Master-Detail Application(iOS template)_03_main.m
// main.m #import <UIKit/UIKit.h>//UIKitk框架提供一系列的Class(类)来建立和管理iPhone OS应用程序的用户界面( UI )接口.应用程序 ...
- Note_Master-Detail Application(iOS template)_06_ YJYDetailViewController.h
// YJYDetailViewController.h #import <UIKit/UIKit.h> @interface YJYDetailViewController : UIV ...
- Note_Master-Detail Application(iOS template)_01_YJYAppDelegate.h
//YJYAppDelegate.h #import <UIKit/UIKit.h> @interface YJYAppDelegate : UIResponder <UIAppli ...
- Differences Between Xcode Project Templates for iOS Apps
Differences Between Xcode Project Templates for iOS Apps When you create a new iOS app project in Xc ...
- iOS开发实践:一个类微博客户端从启动到与用户交互的过程
本文基于数据字典和数据流图两种工具讲述一个完整微博客户端的实现.数据字典和数据流图都可以用来表达线程的执行流程,同时定义了需要的类,是进一步设计类的基础. 数据字典实际上是一张表,表的第一个字段是程序 ...
- 如何在macox下面配置集成ios和android游戏教程
教程截图: 1.准备工作,配置开发环境: 开发环境:mac ox 10.7.3 + xcode4.2 + ndk r7 + eclipse helios 部署环境:中兴v880 root过了 ...
随机推荐
- java 对象 Serializable注意事项
在序列化时,有几点要注意的: 1:当一个对象被序列化时,只保存对象的非静态成员变量,不能保存任何的成员方法和静态的成员变量. 2:如果一个对象的成员变量是一个对象,那么这个对象的数据成员也会被保存. ...
- Machine Learning for hackers读书笔记(三)分类:垃圾邮件过滤
#定义函数,打开每一个文件,找到空行,将空行后的文本返回为一个字符串向量,该向量只有一个元素,就是空行之后的所有文本拼接之后的字符串 #很多邮件都包含了非ASCII字符,因此设为latin1就可以读取 ...
- css读书笔记1:HTML标记和文档结构
块级元素和行内元素:块级元素:上下堆叠,每个块级元素都独立占一行.块级元素的盒子宽度与父元素同宽.行内元素:左右堆叠,只有在空间不足的情况下才会折到下一行显示.行内元素的盒子会收缩包裹其内容,并尽可能 ...
- chrome浏览器下页面顶部出现一条空白解决
最近遇到页面在chrome浏览器下,顶部会出现一条空白的问题.后来知道是bom头的问题. 1.什么是bom头? BOM签名的意思就是告诉编辑器当前文件采用何种编码,方便编辑器识别,但是BOM虽然在编辑 ...
- 周爱民-javascript
从纯化的语言中了解到语言的本质:并以混杂的语言来创造我们的世界,. 程序=算法+结构,动静之间,不变的是本质 了解语言的本质,而不是试图学会一门语言. 本书从语言特性出发,把动态静态.函数 ...
- Java 基础知识点(必知必会其一)
如何将字符串转换为数字? package Day_2; /** * @author Administrator * 功能: 如何将字符串转换为数字? */ public class StringToI ...
- C++编程中const和#define的区别
(1) 编译器处理方式不同 define宏是在预处理阶段展开. const常量是编译运行阶段使用.(2) 类型和安全检查不同 define宏没有类型,不做任何类型检查,仅仅是展开. const常量有具 ...
- jquery表单验证使用插件formValidator
JQuery表单验证使用插件formValidator 作者: 字体:[增加 减小] 类型:转载 时间:2012-11-10我要评论 jquery表单验证使用插件formValidator,可供有需求 ...
- UI-简答的BOL的取值塞值
不知道从什么时候开始,习惯用BOL MODEL来做一些东西的了.某个项目开始正式接触标准主数据的时候,开始了用MAINTAIN BAPI和BUPA的一些FM.然后在一段时间内是以此类的FM来开发的.B ...
- C++统计一段文字中各单词出现的频率
#include <iostream> using namespace std; /* run this program using the console pauser or add y ...