views:

151

answers:

2

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.

+2  A: 

Once you add the row, send -editColumn:row:withEvent:select: to the NSOutlineView:

[outlineView editColumn:0 row:newRowIndex withEvent:nil select:YES];

This is actually an NSTableView message, but since NSOutlineView derives from NSTableView, it should work for outline views too.

John Calsbeek
Ok. So should I put the code in the Data Source of the `NSOutlineView`? Also how would I make it send when you add a row?
Joshua
You would send that message right after sending the message to add the row.
Peter Hosey
Where is the method that sends the message to add a row?
Joshua
It depends on your data source. When you say "when a row is added" we assume that you are the one adding the row—that is, the user clicks a button or something and a row is added, and you're doing the work. If that's the case, then there's where you'd put this call. Otherwise, we need more information.
John Calsbeek
You're right the user adds the row but the App is core data.
Joshua
You should still be creating the object yourself. Follow the `target` and `action` of your button and see where it ends up—that's the code it calls.
John Calsbeek
+1  A: 

A quick psuedocode answer:

In the method that is called when you add a row:

  1. Create the new object
  2. Add it to whatever is storing the objects (an array, tree controller, data store, etc)
  3. Search the store to find the index (row number) of the object that you've added. This is where you get the row number that is called in:-
  4. call -editColumn:row:withEvent:select: as in John Calsbeek's answer.
Abizern
Where is the method that is called when you add a Row, bearing in mind my app is using Core Data?
Joshua
That depends on how you add a row. If you are clicking a button, where is the message going?
Abizern
It's going to the `add` action of my NSTreeController.
Joshua
Try creating a new IBAction method as the target of the button instead, and within this method, call add: on the NSTreeController.
Abizern
Ok, how would I call add, I wouldn't just put `add:`, would I?
Joshua
You should have an IBOutlet for the NSTreeController, send that the add: message.
Abizern
Here's a picture of what I've done but as you can see it gives me some errors. Pic - http://www.grabup.com/uploads/d815d743332910805b1c34e82dcb28e0.png?directI know I need an IBOutlet for the OutlineView but what about the other error and the warning?
Joshua
The first warning is because you've used add without a parameter it should be add:<whatever your new object is>. As for the error - newRowIndex needs to be a number.
Abizern
Oh, I see. This is what It look like now - http://www.grabup.com/uploads/3f48e24cc64451b7e3c025381e43786f.png?direct It works and can add to the Outline View but it doesn't begin editing the new item. Do I need to change the number after `row:`?
Joshua
You still have to follow step three to find out the row that the new item has been added to.
Abizern
Oh Yeh! But how do I 'Search the store to find the index (row number) of the object that i've added'?
Joshua
Have a look at the NSOutlineView documentation, there is an instance method that returns the row for an item.
Abizern
Is it `rowForItem:'? If so, what code would I need to use that instance method?
Joshua
You send the message to the IBOutlet that is the outlineView. Pass the newly created object as an id (don't ask me what that is). Of course, you'll need the id of the object that you create so you'll need to change your code a bit to get that.
Abizern
Ok, here's what I've made it look like. http://www.grabup.com/uploads/ee32102b51a19df3ba6ac791d6d47a2e.png
Joshua
Why are you trying to find the row of the newly added item after you're supposed to use it?
Abizern
So more like this - http://www.grabup.com/uploads/3dac4dbd088829fea9ec820951c2913a.png?direct Then what would I put after row: ?
Joshua
That isn't going to work. Try building it, and then figure out what the errors are saying you need to do.
Abizern
Actually it doesn't give me any errors. But it still doesn't make it edit when a item's been added.
Joshua
What do I need to do now?
Joshua
In the second row, you should be passing an actual object to search for.
Abizern
What object would I need to search for?
Joshua
See step 3 in the answer.
Abizern
So where i put `item` in the code, is that where I am supposed to pass the object to search for?
Joshua
Yes. As the documentation for `rowForItem:` explains.
Abizern
How do i find out the object I need to search for?
Joshua
On Aug 14th your comment - "Oh Yeh! But how do I 'Search the store to find the index (row number) of the object that i've added'" - so you're looking for the object that you added to the outline view.
Abizern
Oh Ok. I know what to do now. So like this - http://www.grabup.com/uploads/34f1591e8a25a9566a361bc41a4411b4.png?direct Right?
Joshua
Nope. You're not going to be able to do this in three lines; you're adding a static string rather than the actual CD object and you're not even searching for it. You need to check that the row returned is valid and then pass it to the search method as the `row`, instead of just `1`.
Abizern
Oh. Whats the actual Core Data object then?
Joshua