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
2009-08-12 17:37:24
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
2009-08-13 06:21:09
You would send that message right after sending the message to add the row.
Peter Hosey
2009-08-13 06:32:24
Where is the method that sends the message to add a row?
Joshua
2009-08-13 14:50:33
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
2009-08-13 15:14:05
You're right the user adds the row but the App is core data.
Joshua
2009-08-13 15:38:22
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
2009-08-14 14:25:33
+1
A:
A quick psuedocode answer:
In the method that is called when you add a row:
- Create the new object
- Add it to whatever is storing the objects (an array, tree controller, data store, etc)
- 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:-
- call
-editColumn:row:withEvent:select:
as in John Calsbeek's answer.
Abizern
2009-08-13 14:53:15
Where is the method that is called when you add a Row, bearing in mind my app is using Core Data?
Joshua
2009-08-13 15:39:39
That depends on how you add a row. If you are clicking a button, where is the message going?
Abizern
2009-08-13 16:08:29
Try creating a new IBAction method as the target of the button instead, and within this method, call add: on the NSTreeController.
Abizern
2009-08-13 17:12:19
You should have an IBOutlet for the NSTreeController, send that the add: message.
Abizern
2009-08-13 18:29:47
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
2009-08-14 05:59:40
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
2009-08-14 09:02:06
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
2009-08-14 10:18:41
You still have to follow step three to find out the row that the new item has been added to.
Abizern
2009-08-14 14:32:38
Oh Yeh! But how do I 'Search the store to find the index (row number) of the object that i've added'?
Joshua
2009-08-14 15:07:03
Have a look at the NSOutlineView documentation, there is an instance method that returns the row for an item.
Abizern
2009-08-14 15:49:41
Is it `rowForItem:'? If so, what code would I need to use that instance method?
Joshua
2009-08-14 17:06:31
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
2009-08-14 18:28:42
Ok, here's what I've made it look like. http://www.grabup.com/uploads/ee32102b51a19df3ba6ac791d6d47a2e.png
Joshua
2009-08-14 19:04:10
Why are you trying to find the row of the newly added item after you're supposed to use it?
Abizern
2009-08-14 20:37:21
So more like this - http://www.grabup.com/uploads/3dac4dbd088829fea9ec820951c2913a.png?direct Then what would I put after row: ?
Joshua
2009-08-15 07:09:33
That isn't going to work. Try building it, and then figure out what the errors are saying you need to do.
Abizern
2009-08-15 15:10:48
Actually it doesn't give me any errors. But it still doesn't make it edit when a item's been added.
Joshua
2009-08-15 16:29:26
In the second row, you should be passing an actual object to search for.
Abizern
2009-08-16 09:20:20
So where i put `item` in the code, is that where I am supposed to pass the object to search for?
Joshua
2009-08-17 06:36:59
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
2009-08-17 16:13:53
Oh Ok. I know what to do now. So like this - http://www.grabup.com/uploads/34f1591e8a25a9566a361bc41a4411b4.png?direct Right?
Joshua
2009-08-17 16:59:12
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
2009-08-17 17:33:37