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环境高级编程第三版>,在此 ...
随机推荐
- 【ShaderToy】基础篇之再谈抗锯齿(antialiasing,AA)
写在前面 在之前的基础篇中,我们讲到了在绘制点线时如何处理边缘的锯齿,也就是使用smoothstep函数.而模糊参数是一些定值,或者是跟屏幕分辨率相关的数值,例如分辨率宽度的5%等等.但这种方法其实是 ...
- java中List接口的实现类 ArrayList,LinkedList,Vector 的区别 list实现类源码分析
java面试中经常被问到list常用的类以及内部实现机制,平时开发也经常用到list集合类,因此做一个源码级别的分析和比较之间的差异. 首先看一下List接口的的继承关系: list接口继承Colle ...
- 剑指Offer——全排列递归思路
剑指Offer--全排列递归思路 前言 全排列,full permutation, 可以利用二叉树的遍历实现.二叉树的递归遍历,前中后都简洁的难以置信,但是都有一个共同特点,那就是一个函数里包含两次自 ...
- oralce 查看是否启动 登陆 创建用户 常用命令小记
最简单看进程有没有: ps -ef | grep ora 其次用oracle的的命令查看,比如: su - oracle sqlplus / as sysdba 看能连进数据库不. 创建用户和表空间: ...
- 套接字工厂——ServerSocketFactory
接收器Acceptor在接收连接的过程中,根据不同的使用场合可能需要不同的安全级别,例如在支付相关的交易就必须对信息加密后再发送,这其中还涉及到密钥协商的过程,而在另外一些普通场合则无需对报文加密.反 ...
- Cookie 进阶
Cookie作为一个客户端技术被广泛的应用着.我今天也来谈一谈我对Cookie的理解. 先来一个小菜(实现"上次登录时间") 具体的思路如下: 通过request.getCooki ...
- 可视化分析工具Cytoscape使用记录
最近项目要使用到可视化分析工具Cytoscape,所以会花费很多的时间跟精力来整理Cytoscape软件使用和开发的相关资料,希望写下的文章能减少有兴趣的同行学习跟开发所走的弯路时间.同时也是因为百度 ...
- Docker教程:dokcer的配置和命令
http://blog.csdn.net/pipisorry/article/details/50803028 Docker命令查询 终端运行docker命令,它会打印所有可用的命令列表及使用描述:# ...
- java方法重写和super关键字
//java方法重写和super关键字 //在继承中,其实就是子类定义了和父类同名的方法 //就是方法,属性都是相通的 //重写限制: //被子类重写的方法不能拥有比父类方法更加严格的权限 //sup ...
- Web开发技术的演变
原文出处: WildFly 欢迎分享原创到伯乐头条 受到好文<Web开发的发展史>(英文)激发的灵感,写下我对web开发技术的认识. 1. 静态页面时代 大学时候,上机还得换卡穿拖鞋, ...