该版本的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 解答的更多相关文章

  1. cocoa编程第4版 8.5 挑战1 解答

    看似简单,其实也很简单,但开始思路想错了:还上网查了一下,有网友说是将Array Controller的Keys中的personName改为personName.length,好像完全不起作用. 后来 ...

  2. 苹果开发之COCOA编程(第三版)上半部分

    第一章:什么是Cocoa 1.1 历史简介 1.2 开发工具:Xcode.Interface Builder(一个GUI构建工具).在它们内部,使用gcc为编译器来编译代码,并使用gdb来查找错误 1 ...

  3. 苹果开发之COCOA编程(第三版)下半部分

    第十八章:Image和鼠标事件 1.NSResponderNSView继承自NSResponder类.所有的事件处理方法都定义在NSResponder类中.NSResponder申明了如下方法:- ( ...

  4. 《苹果开发之Cocoa编程》挑战2 创建一个数据源 练习

    <苹果开发之Cocoa编程>第4版 P87 创建一个to-do list应用程序,在文本框中输入任务.当用户单击Add按钮时,添加字符串到一个变长队列,新任务就出现在list的末尾. 关键 ...

  5. 《苹果开发之Cocoa编程》挑战1 创建委托 练习

    <苹果开发之Cocoa编程>第4版 P87 新建一个单窗口应用程序,设置某对象为窗口的委托,当用户调整窗口尺寸时,确保窗口高度为宽度的2倍. 需要实现的委托方法为:-(NSSize)win ...

  6. 教孩子学编程 python语言版PDF高清完整版免费下载|百度云盘|Python入门

    百度云盘:教孩子学编程 python语言版PDF高清完整版免费下载 提取码:mnma 内容简介 本书属于no starch的经典系列之一,英文版在美国受到读者欢迎.本书全彩印刷,寓教于乐,易于学习:读 ...

  7. 《UNIX网络编程(第3版)》unp.h等源码文件的编译安装

    操作系统:Mac OS X 10.11.5 1.下载书中的源代码:点击下载 2.切换到解压后的目录 unpv13e,先查看下 README,依次执行: ./configure cd lib make ...

  8. Cocoa编程开发者手册

    Cocoa编程开发者手册(Objective-C权威著作超一流翻译阵容) [美] 奇斯纳尔(Chisnall,D.)  著 霍炬等 译 ISBN 978-7-121-12239-2 2013年7月出版 ...

  9. 【转】apue《UNIX环境高级编程第三版》第一章答案详解

    原文网址:http://blog.csdn.net/hubbybob1/article/details/40859835 大家好,从这周开始学习apue<UNIX环境高级编程第三版>,在此 ...

随机推荐

  1. Protobuf-net判断字段是否有值

    Protobuf-net判断字段是否有值Unity3d使用Protobuf-net序列化数据与服务器通信,但是发现默认情况下,Protobuf-net生成的cs文件中没有接口判断可选参数是否有值.需有 ...

  2. Java的多态及注意事项

    什么是多态: 多态不但能够改善代码的组织结构和可读性,还能够创建可扩展的程序.在Java中,所有的方法都是通过动态绑定实现多态的.将一个方法调用同一个方法主体关联起来被称作绑定.动态绑定的含义是在运行 ...

  3. 【Android应用开发】Android Studio 错误集锦 -- 将所有的 AS 错误集合到本文

    . 一. 编译错误 1. "AndroidManifest.xml file not found" 错误 (1) 报错信息 报错信息 : -- Message Make : Inf ...

  4. Swift基础之Animation动画研究

    最近研究了一下,Swift语言中关于Animation动画的实现学习,分两次进行相关内容的讲解 用表格列出各种动画情况 Demo首页显示展示了一种动画显示方式,代码如下: //绘画装饰    func ...

  5. reactor线程阻塞引起故障

    大致线程模型: jstack打印JVM堆栈,可以看到reactor线程阻塞了,导致它对应的前端连接无法使用.阻塞在了oracle驱动rollback动作,这里其实是因为oracle驱动为了保证串行请求 ...

  6. Android 5.x 权限问题解决方法

    android 5.x开始,引入了非常严格的selinux权限管理机制,我们经常会遇到因为selinux权限问题造成的各种avc denied困扰.  本文结合具体案例,讲解如何根据log来快速解决9 ...

  7. Android中JNI编程详解

    前几天在参加腾讯模拟考的时候,腾讯出了一道关于JNI的题,具体如下: JNI本身是一个非常复杂的知识,但是其实对于腾讯的这道题而言,如果你懂JNI,那么你可能会觉得这道题非常简单,就相当于C语言中的h ...

  8. 【Unity Shaders】Vertex Magic —— 访问顶点颜色

    本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...

  9. 【一天一道LeetCode】#141. Linked List Cycle

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  10. 深度剖析malloc、free和new、delete

    1.malloc,free是C语言的函数,而new,delete是操作符,属于C++的语法,一定注意这两个不再是函数了,而是操作符. 2.malloc和new对于分配基础类型变量和数组变量,它们除了语 ...