Skip to content

Double Clicks in Table Views

2008 July 9
by Tim Isted

Previously we’ve covered multiple windows and managed object contexts in Core Data applications. In this fairly brief article we’ll cover how to open a display/editing window for an object when that object is double-clicked in a table view.

– Edit – The example project for this post is available to download from here:

This article assumes you’re working with the CoreDataMultiWin project from either of the earlier posts on this site.

First, open your MyDocument.h header file and add an IBOutlet for the table view:

@interface MyDocument : NSPersistentDocument {
	IBOutlet NSArrayController *peopleArrayController;
	IBOutlet NSArrayController *departmentsArrayController;
	IBOutlet NSTableView *peopleTableView;
	AddDepartmentController *_addDeptCtl;
	AddEmployeeController *_addEmpCtl;

Open the MyDocument.nib file in Interface Builder and connect the outlet to the employees table view in the Window of the nib.

Back in Xcode, open MyDocument.m and write a method that will be called when the table is double-clicked:

- (IBAction)doubleClickAction:(id)sender
	[self openPersonWindow:sender];

This method simply calls the openPersonWindow: method that we wrote to open selected employees when the ‘Open Employee Window’ button in the window is clicked. It will work just as well here since that method asks the array controller which people are currently selected and then opens an editing window for them. When you double click on a person, that person is selected by the first click so we can use the same code.

In the MyDocument windowControllerDidLoadNib:, insert code that sets the doubleAction of the table view to be the selector for our doubleClickAction: method:

	[peopleTableView setTarget:self];
	[peopleTableView setDoubleAction:@selector(doubleClickAction:)];

This code sets the MyDocument object (self) to be the target of the table view so that when a double click occurs, the specified code in MyDocument gets called.

If you now build and run the application, you should find that double-clicking on a person in the table view opens a new window with that person’s details. You should also check that only one window per person is ever opened, no matter how many times that person is double-clicked.

4 Responses leave one →
  1. intrntmn permalink
    July 29, 2008

    What would be the best method for making sure that only one window per person is opened? I have done this in my own application, but it “doesn’t feel” like the best way. Thanks.

  2. July 29, 2008

    The sample project has code in the openPersonWindow: method, also described towards the end of the article Multiple Windows with Core Data on this site.

    Essentially, the way I accomplish this is to make the person window controller KVC compliant for the person object it is showing. It’s then possible to cycle through the document object’s open windows, see if they respond to your person accessor (ie they are a person window) and if so, check whether their person object matches the one that’s been double-clicked. If it is, it brings that window to the front, otherwise it opens a new one.

Trackbacks and Pingbacks

  1. Blog @ Tim Isted » Blog Archive » Sample project code now available for all posts
  2. Passing Curiosity » Blog Archive » Resources for Learning Cocoa

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS