What to subclass in NSArrayController to enable immediate editing of added rows?
What to subclass in NSArrayController to enable immediate editing of added rows?
- Subject: What to subclass in NSArrayController to enable immediate editing of added rows?
- From: David Hirsch <email@hidden>
- Date: Mon, 30 Nov 2009 19:57:53 -0800
I have read about this online, but I can't find a solution that (a)
works and (b) seems wise.
I'm trying to have my table immediately enable editing of added
items. The NSTableView is bound to an NSArrayController. I've
subclassed NSArrayController, and tried this first:
- (void)add:(id)sender {
[super add:sender];
NSInteger numRows = [myTable numberOfRows];
NSIndexSet *indexes = [NSIndexSet indexSetWithIndex:numRows-1];
[myTable selectRowIndexes:indexes byExtendingSelection:NO];
[myTable editColumn:0 row:numRows-1 withEvent: nil select:YES];
}
Now, leaving aside the issues surrounding sorting of the arrays, and
possibly getting the WRONG item selected for editing, this fails
because, as the docs state, the results of the add: method are delayed
until the next runloop. So a cell starts editing for an instant, but
then as soon as the next runloop occurs, the new cell really gets
added, and then is not enabled for editing.
Here is what I tried next. It works, but I don't like it, because it
doesn't call [super add], which seems like a bad idea - what if new
functionality gets put into that method in the future?
- (void)add:(id)sender {
// Adapted from http://stackoverflow.com/questions/844933/move-focus-to-newly-added-record-in-an-nstableview
//Try to end any editing that is taking place in the table view
NSWindow *w = [myTable window];
BOOL endEdit = [w makeFirstResponder:w];
if(!endEdit)
return;
//Create a new object to add to your NSTableView; replace NSString with
//whatever type the objects in your array controller are
Room *new = [self newObject];
//Add the object to your array controller
[self addObject:new];
[new release];
//Rearrange the objects if there is a sort on any of the columns
[self rearrangeObjects];
//Retrieve an array of the objects in your array controller and
calculate
//which row your new object is in
NSArray *array = [self arrangedObjects];
int row = [array indexOfObjectIdenticalTo:new];
//Begin editing of the cell containing the new object
[myTable editColumn:0 row:row withEvent:nil select:YES];
}
I've read methods of handling this (e.g., the link from which the
above code was adapted, http://www.friday.com/bbum/2006/05/18/core-data-array-controller-edit-on-insert/
) that rely on actions in the document subclass, but I have to do this
for a number of different tables/controllers in my document and I'd
like to encapsulate the functionality in the NSArrayController
subclass instead of the document.
Should I be subclassing a different method that is not delayed?
addObject? Is there a better strategy altogether?
Thanks,
Dave
============================
Dave Hirsch
Associate Professor
Department of Geology
Western Washington University
persistent email: email@hidden
http://www.davehirsch.com
voice: (360) 389-3583
aim: email@hidden
vCard: http://almandine.geol.wwu.edu/~dave/personal/DaveHirsch.vcf
============================
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden