Re: Starting editing for a Row as soon as it is Added.
Re: Starting editing for a Row as soon as it is Added.
- Subject: Re: Starting editing for a Row as soon as it is Added.
- From: "I. Savant" <email@hidden>
- Date: Wed, 19 Aug 2009 10:50:11 -0400
On Aug 19, 2009, at 10:30 AM, Joshua Garnham wrote:
I have an NSOutlineView and what I want to happen is that when a row
is added I want the row that has been added to Start Editing
immediately
like when you double click on a row.
...
- (IBAction)add:(id)sender {
[treeController add:@"New Item"];
[outlineView rowForItem:(id)@"New Item"];
[outlineView editColumn:0 row:1 withEvent:nil select:YES];
}
There are several things wrong here.
First, since you're using a tree controller, this implies Bindings.
You'll need to read a whole lot (Cocoa Bindings Programming Topics,
Key-Value Observing Programming Guide, Key-Value Coding Programming
Guide ... and anything that describes how the run loop works) to
understand this, but:
By the time you're asking for the new item's row, your item hasn't
been added and refreshed in the controller yet. I won't delve into
detail because honestly you're just going to have to read. Best to
figure out how to do this stuff without Bindings (and advanced Cocoa
topic) before using it.
Next:
[treeController add:@"New Item"];
Read the NSTreeController reference's entry on the "-add:" method.
You can see on the very first line that the argument (where you're
passing a "New Item" string) is expected to be the sender, not an
object to add. To understand this, search the docs and read anything
related to the Target/Action mechanism in Cocoa.
Also, if you want to directly insert something into a tree
controller, you'll want the -insertObject:atArrangedObjectIndexPath:
or -insertObjects:atArrangedObjectIndexPaths: methods.
Next:
[outlineView rowForItem:(id)@"New Item"];
I'm not sure what you're expecting to happen here. You're asking
the outline view for the item's row but not storing the result into
anything (like, say, an integer).
Also, because of your mistake above, you've only added "an
object" (which the tree controller creates from whatever class or
entity you told it to). You have no reference to that created object.
This line (with the string you passed in) amounts to "give me the
index of an object you don't have because I didn't add it, and don't
do anything with the result.
Next:
[outlineView editColumn:0 row:1 withEvent:nil select:YES];
Here you're ignoring any row information you might otherwise get
and always saying "edit the first column of the second item".
One problem is that rows are zero-based, so "row 1" is really "the
second row". The first row is row 0.
Another problem is, as I said, unless there are already at least
two items in the tree (either two at root level, or one at root with
one expanded child), there's nothing *at* col:0, row:1, so no editing
will begin. If there *is* something already there, it won't be the
thing you just inserted in this block of code because the controller
hasn't updated yet.
...
In short, your code is completely broken in almost every imaginable
way while still being able to be compiled. :-D Programming languages
and environments are difficult topics and take an investment in time
before you can get off the ground.
I really think you should start with the basics. It may be that you
don't realize that Bindings (and its associated technologies) is a
more advanced Cocoa subject, but you're trying to tackle some pretty
tough stuff and the "if I learn the hard stuff, the easy stuff will
come naturally" approach is ... well ... a bad idea. :-)
In summary, you should definitely (re)read the basic introductory
Cocoa information and any guides it references. The target/action
mechanism is Cocoa 101 and you don't appear to have grasped it yet.
Don't forget there are lots of example applications in your /
Developer/Examples folder. One of them specifically shows how to use
NSOutlineView ...
Good luck and happy reading!
--
I.S.
_______________________________________________
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