
该版本的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:%@",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.6 挑战2 解答所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)