cocoa编程第4版 8.6 挑战2 解答
该版本的RaiseMan不用Array Controller,全部手写代码。
要注意的有以下几点:
1.TableView每列的sort设置和AC版的相同,但要手写排序代理方法
2.TableView和add、remove按钮的绑定和一般cocoa程序相同
3.需要添加TableView每列的id
4.需要手写TableView的DataSource和Delegate的相关方法
5.Person类和AppDelegate类方法和AC版的相同
代码如下:
Document.h
//
// Document.h
// RaiseMan_No_AC
//
// Created by kinds on 15/6/29.
// Copyright (c) 2015年 hopy. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface Document : NSDocument{
NSMutableArray *employees;
}
@property (weak) IBOutlet NSButton *removeButton;
@property (weak) IBOutlet NSTableView *tableView;
- (IBAction)remove:(id)sender;
- (IBAction)add:(id)sender;
@end
Document.m
//
// Document.m
// RaiseMan_No_AC
//
// Created by kinds on 15/6/29.
// Copyright (c) 2015年 hopy. All rights reserved.
//
#import "Document.h"
#import "Person.h"
@interface Document ()
@end
@implementation Document
- (instancetype)init {
self = [super init];
if (self) {
// Add your subclass-specific initialization here.
employees = [NSMutableArray array];
}
return self;
}
- (void)windowControllerDidLoadNib:(NSWindowController *)aController {
[super windowControllerDidLoadNib:aController];
// Add any code here that needs to be executed once the windowController has loaded the document's window.
}
+ (BOOL)autosavesInPlace {
return YES;
}
- (NSString *)windowNibName {
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return @"Document";
}
-(void)awakeFromNib{
[_removeButton setEnabled:NO];
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
// Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
[NSException raise:@"UnimplementedMethod" format:@"%@ is unimplemented", NSStringFromSelector(_cmd)];
return nil;
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
// Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
// You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
// If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
[NSException raise:@"UnimplementedMethod" format:@"%@ is unimplemented", NSStringFromSelector(_cmd)];
return YES;
}
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tv{
return [employees count];
}
/*
-(BOOL)tableView:(NSTableView*)tv shouldSelectRow:(NSInteger)row{
NSLog(@"%s:selected row is %ld",__func__,row);
return YES;
}
*/
-(void)tableView:(NSTableView *)tv sortDescriptorsDidChange:(NSArray *)oldDescriptors{
NSArray *sort_descriptors = [tv sortDescriptors];
NSLog(@"%s:%@",__func__,sort_descriptors);
[employees sortUsingDescriptors:sort_descriptors];
[tv reloadData];
}
-(void)tableViewSelectionDidChange:(NSNotification*)notification{
NSLog(@"entry %s",__func__);
if([[_tableView selectedRowIndexes] count] == 0)
[_removeButton setEnabled:NO];
else
[_removeButton setEnabled:YES];
}
-(id)tableView:(NSTableView*)tv objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row{
NSString *col_id = [tableColumn identifier];
Person *person = [employees objectAtIndex:row];
return [person valueForKey:col_id];
}
-(void)tableView:(NSTableView*)tv setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row{
NSString *col_id = [tableColumn identifier];
Person *person = [employees objectAtIndex:row];
[person setValue:object forKey:col_id];
}
- (IBAction)remove:(id)sender {
NSLog(@"entry %s",__func__);
NSIndexSet *rows = [_tableView selectedRowIndexes];
//NSLog(@"rows is %@",rows);
if([rows count] == 0) {
NSBeep();
return;
}
[employees removeObjectsAtIndexes:rows];
[_tableView reloadData];
}
- (IBAction)add:(id)sender {
NSLog(@"entry %s",__func__);
Person *employee = [Person new];
[employees addObject:employee];
[_tableView reloadData];
}
@end
cocoa编程第4版 8.6 挑战2 解答的更多相关文章
- cocoa编程第4版 8.5 挑战1 解答
看似简单,其实也很简单,但开始思路想错了:还上网查了一下,有网友说是将Array Controller的Keys中的personName改为personName.length,好像完全不起作用. 后来 ...
- 苹果开发之COCOA编程(第三版)上半部分
第一章:什么是Cocoa 1.1 历史简介 1.2 开发工具:Xcode.Interface Builder(一个GUI构建工具).在它们内部,使用gcc为编译器来编译代码,并使用gdb来查找错误 1 ...
- 苹果开发之COCOA编程(第三版)下半部分
第十八章:Image和鼠标事件 1.NSResponderNSView继承自NSResponder类.所有的事件处理方法都定义在NSResponder类中.NSResponder申明了如下方法:- ( ...
- 《苹果开发之Cocoa编程》挑战2 创建一个数据源 练习
<苹果开发之Cocoa编程>第4版 P87 创建一个to-do list应用程序,在文本框中输入任务.当用户单击Add按钮时,添加字符串到一个变长队列,新任务就出现在list的末尾. 关键 ...
- 《苹果开发之Cocoa编程》挑战1 创建委托 练习
<苹果开发之Cocoa编程>第4版 P87 新建一个单窗口应用程序,设置某对象为窗口的委托,当用户调整窗口尺寸时,确保窗口高度为宽度的2倍. 需要实现的委托方法为:-(NSSize)win ...
- 教孩子学编程 python语言版PDF高清完整版免费下载|百度云盘|Python入门
百度云盘:教孩子学编程 python语言版PDF高清完整版免费下载 提取码:mnma 内容简介 本书属于no starch的经典系列之一,英文版在美国受到读者欢迎.本书全彩印刷,寓教于乐,易于学习:读 ...
- 《UNIX网络编程(第3版)》unp.h等源码文件的编译安装
操作系统:Mac OS X 10.11.5 1.下载书中的源代码:点击下载 2.切换到解压后的目录 unpv13e,先查看下 README,依次执行: ./configure cd lib make ...
- Cocoa编程开发者手册
Cocoa编程开发者手册(Objective-C权威著作超一流翻译阵容) [美] 奇斯纳尔(Chisnall,D.) 著 霍炬等 译 ISBN 978-7-121-12239-2 2013年7月出版 ...
- 【转】apue《UNIX环境高级编程第三版》第一章答案详解
原文网址:http://blog.csdn.net/hubbybob1/article/details/40859835 大家好,从这周开始学习apue<UNIX环境高级编程第三版>,在此 ...
随机推荐
- springMVC源码分析--HandlerInterceptor拦截器调用过程(二)
在上一篇博客springMVC源码分析--HandlerInterceptor拦截器(一)中我们介绍了HandlerInterceptor拦截器相关的内容,了解到了HandlerInterceptor ...
- JBOSS EAP 6 系列四 EJB实现——调用(贯穿始终的模块)
本文主要介绍在JBOSS EAP 6.2(或者JBOSS AS7)中模块是如何贯穿EJB实现的始终.延续上一博文<认识模块的使用>的话题继续聊JBOSS做为模块申明式容器的这一特性在EJB ...
- hive分组排序 取top N
pig可以轻松获取TOP n.书上有例子 hive中比较麻烦,没有直接实现的函数,可以写udf实现.还有个比较简单的实现方法: 用row_number,生成排名序列号.然后外部分组后按这个序列号多虑, ...
- iOS中NSTimer的invalidate调用之后
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交 ...
- 【shell点滴】参数变量
参数变量故名思议就是用来操作输入参数的变量,知道用户输入了哪些参数,才可以进行相应的处理. 参数变量 作用 $1,$2- 取第几个参数的意思 $* 取出所有的参数,解析参数的分割符环境变量 IFS 来 ...
- ROS(indigo)使用Qt Creator Plug in即ros_qtc_plugin
更为详细版本请参考: http://blog.csdn.net/zhangrelay/article/details/52214411 结合看更为具体. 首先,先上原版参考: 1 http://wik ...
- 美国康奈尔大学BioNB441元胞自动机MATLAB应用
美国康奈尔大学BioNB441在Matlab中的元胞自动机 介绍 元胞自动机(CA)是用于计算计划利用当地的规则和本地通信.普遍CA定义一个网格,网格上的每个点代表一个有限数量的状态中的细胞.过渡规则 ...
- Dynamics CRM 在报表中获取当前登陆用户的guid
<span style="font-size:18px;">CRM提供函数,只需在报表中调用即可.</span> <pre class="s ...
- tomcat服务器虚拟目录的映射方式
lWEB应用程序指供浏览器问的程序,通常也简称为web应用 l l一个web应用由多个静态web资源和动态web资源组成,如: •html.css.js文件 •jsp文件.servlet程序.支持ja ...
- 精通CSS+DIV网页样式与布局--图片效果
提到图片效果,小伙伴们可能会想到美图秀秀,ps等,这些软件都是款非常不错的照片处理软件,包括常用的:黑白,增强,高斯,高对比,夜视,老照片和铅笔画等等.不管你是否是专业的 照片拍摄人员,我们都可以通过 ...