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环境高级编程第三版>,在此 ...
随机推荐
- SpriteKit:在场景过渡中暂停动画
Pausing Scenes During a Transition 你应该意识到两个重要的SKTrnsition属性在场景之间的过渡中. 它们是pausesIncomingScene和pausesO ...
- Android的log日志知识点剖析
log类的继承结构 Log public final class Log extends Object java.lang.Object ↳ android.util.Log log日志的常用方法 分 ...
- [django]用fastcgi部署
django官方已经开始弃用fastcgi来部署django应用了,作为以前使用过的用户,还是贴一个配置,用来做纪念吧.. 项目下 #! /bin/sh case "$@" in ...
- HDFS的写数据过程分析
HDFS的写数据过程分析 我们通过FileSystem类可以操控HDFS, 那我们就从这里开始分析写数据到HDFS的过程. 在我们向 HDFS 写文件的时候,调用的是 FileSystem.creat ...
- java虚拟机 jvm 出入java栈 栈空间内存分配
java栈空间是一块线程私有的内存空间,java堆和程序数据密切相关,那么java栈就是和线程执行密切相关.线程最基本的执行行为就是函数的调用.每次函数调用其实是通过java栈传递数据的. 数据结构中 ...
- 服务端技术进阶(八)GitHub入门篇
服务端技术进阶(八)GitHub入门篇 前言 在投递简历的过程中,发现有的公司会要求填写自己的GitHub地址,而自己却还没有GitHub帐号,准确点说是自己还不太会使用GitHub.(貌似开源社区中 ...
- [java面试]宇信易诚 广州分公司 java笔试题目回忆录
本文地址:http://blog.csdn.net/sushengmiyan/article/details/28479895 作者:sushengmiyan -------------------- ...
- SDL2源代码分析4:纹理(SDL_Texture)
===================================================== SDL源代码分析系列文章列表: SDL2源代码分析1:初始化(SDL_Init()) SDL ...
- Java中常用的正则表达式
常用的正则表达式 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 匹配双字节字符(包括汉字在内):[^\x00-\xff] 匹配空行的正则表达式:\n[\s| ]*\r 匹配HTML标记的 ...
- eclipse进行关联代码
关联代码找了半天,终于找到了详细解说的: http://blog.csdn.net/long2010yu2010/article/details/8497505