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.5 挑战1 解答


下一篇:6. Python3源码—List对象