Double Clicks in Table Views

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: coredatamultiwin3.zip.

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.

  • Share/Bookmark

4 Comments

  1. [...] only a short article, a full project demonstrating Double Clicks in Table Views can be found here: [...]

  2. intrntmn says:

    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.

  3. Tim Isted says:

    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.

  4. [...] Isted has posted some excellent Core Data tutorials on his blog Blog @ Tim [...]

Leave a Reply