Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [48]

By Root 888 0

- (void)tableView:(NSTableView *)tableView

sortDescriptorsDidChange:(NSArray *)oldDescriptors

Thus, if you have a mutable array that holds the information for a table view, you can implement the method like this:

- (void)tableView:(NSTableView *)tableView

sortDescriptorsDidChange:(NSArray *)oldDescriptors

{

NSArray *newDescriptors = [tableView sortDescriptors];

[myArray sortUsingDescriptors:newDescriptors];

[tableView reloadData];

}

And voila!, sorting in your application.

Challenge 1


Make the application sort people based on the number of characters in their names. You can complete this challenge using only Interface Builder—the trick is to use a key path. (Hint: Strings have a length method.)

Challenge 2


In the first edition of this book, readers created the RaiseMan application without using NSArrayController or the bindings mechanism. (These features were added in Mac OS X 10.3.) To do so, readers used the ideas from previous chapters. The challege, then, is to rewrite the RaiseMan application without using NSArrayController or the bindings mechanism. Bindings often seem rather magical, and it is good to know how to do things without resorting to magic.

Be sure to start afresh with a new project—in the next chapter, we will build on your existing project.

The Person class will stay exactly the same. In RMDocument.xib, you will set the identifier of each column to be the name of the variable that you would like displayed (use the Attributes Inspector in Interface Builder). Then, the RMDocument class will be the dataSource of the table view and the target of the Create New Employee and Delete buttons. RMDocument will have an array of Person objects that it displays. To get you started, here is RMDocument.h:

#import

@class Person;

@interface RMDocument : NSDocument

{

NSMutableArray *employees;

IBOutlet NSTableView *tableView;

}

- (IBAction)createEmployee:(id)sender;

- (IBAction)deleteSelectedEmployees:(id)sender;

@end

Here are the interesting parts of RMDocument.m:

- (id)init

{

self = [super init];

if (self) {

employees = [NSMutableArray array];

}

return self;

}

#pragma mark Action methods

- (IBAction)deleteSelectedEmployees:(id)sender

{

// Which row is selected?

NSIndexSet *rows = [tableView selectedRowIndexes];

// Is the selection empty?

if ([rows count] == 0) {

NSBeep();

return;

}

[employees removeObjectsAtIndexes:rows];

[tableView reloadData];

}

- (IBAction)createEmployee:(id)sender

{

Person *newEmployee = [[Person alloc] init];

[employees addObject:newEmployee];

[tableView reloadData];

}

#pragma mark Table view dataSource methods

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView

{

return [employees count];

}

- (id)tableView:(NSTableView *)aTableView

objectValueForTableColumn:(NSTableColumn *)aTableColumn

row:(NSInteger)rowIndex

{

// What is the identifier for the column?

NSString *identifier = [aTableColumn identifier];

// What person?

Person *person = [employees objectAtIndex:rowIndex];

// What is the value of the attribute named identifier?

return [person valueForKey:identifier];

}

- (void)tableView:(NSTableView *)aTableView

setObjectValue:(id)anObject

forTableColumn:(NSTableColumn *)aTableColumn

row:(NSInteger)rowIndex

{

NSString *identifier = [aTableColumn identifier];

Person *person = [employees objectAtIndex:rowIndex];

// Set the value for the attribute named identifier

[person setValue:anObject forKey:identifier];

}

Once you have it working, be sure to add sorting!

Chapter 9. NSUndoManager


Using NSUndoManager, you can add undo capabilities to your applications in a very elegant manner. As objects are added, deleted, and edited, the undo manager keeps track of all messages that must be sent to undo these changes. As you invoke the undo mechanism, the undo manager keeps track of all messages that must be sent to redo those changes. This mechanism works by utilizing two stacks of NSInvocation objects.

This is a pretty heavy

Return Main Page Previous Page Next Page

®Online Book Reader