Table View, single column type directly into row, need example

I am trying to build a user interface item where a user creates a list that is maintained in a Table View (single column). So far the examples I’ve found want the editing of the new row to be done in text fields outside of the table (I’m thinking of the “Table” example that comes in the Developer examples folder).

I want an “add” button but instead of copying text values from outside text fields I want a new row to be added to the table with the cursor in the text field of the new row so the user can type their value directly into the table.

Is anyone aware of an example like this they can pass along?

If I am understanding you correctly give this a try. This example assumes that you have the table tied to a data source and your “add” button is linked to the handler “create_table_row()”

on create_table_row()
	tell data source "the data source"
		set update views to false
		set theRow to make new data row at end of data rows
		set update views to true
	end tell
	set theWindow to window 1
	set theTable to table view 1 of scroll view 1 of theWindow
	set first responder of theWindow to theTable
	set selected row of theTable to 1
	call method "editColumn:row:withEvent:select:" of object theTable with parameters {0, 0, 0, 1}
end create_table_row

More information on the method used can be found here - NSTableView Reference

For quick reference though the parameters are {column index, row index, an NSevent, a boolean}

The column and row index are both zero based. The NSevent takes any value, but I would say stick with 0 because it works.

Also note that if you do not select the row prior to calling the event a exception will be raised.

Obviously if this handler will be called more than once you will need to track which row you are on and pass that in since subsequent row creations occur descending and you don’t want to keep selecting your initial row.

EDIT - If you were to

set theRow to make new data row at beginning of data rows

you could avoid having to track the row you are on. I guess it all depends on your preference for the handling of the rows.

Never seen that sort of thing outside dedicated database applications like FileMaker or Microsoft Access.

I can think of a few ways to fake that effect…maybe…in FaceSpan but it would be, to paraphrase the Matrix, “the ugliest hack I’ve ever done.”

Best idea I could come up with would be to have a standard OS list view portal with data entry fields layered on top of the bottom edge, or perhaps kiss-fit to the bottom of the portal and try to fool the user into thinking they are a single UI element with common graphical look. then additionally somehow control the scrolling of the portal portion so any adds are magically second from the bottom with the “blanks” in the form of your data entry fields on the bottom.

You’d have to make sure the data entry had a trigger…like focus-out or a “add” button or something to let the interface know the new entry was ready to be moved to the portal.

This would mimick the “Access effect.” MS Access list views generally have a blank row at the bottom of a list that is a data-entry row.

Like I said, very ugly hack, and I’m not sure how many interface guidelines it trods all over. And the code to make it work is beyond my time constraints.

Thank you, guys, for the replies. I know I’ve seen something like what I envision before, but it sounds like the implementing is pretty complicated. The goal is for the user to populate a list of possibilities that the program uses when it does a process. Since the list is fairly “simple”, just one column in a table I was trying to simplify the entry into this list by having the user type directly into the table instead of having a separate text entry field.

Maybe I could make it appear simple and hide the text entry field into a text field in a sheet. Like in the Network section of System Preferences (OS 10.5). On the left side is the list of the network interfaces and when you click the + button a sheet appears asking you for the Service Name. That seems like the best way to go, as I can copy/paste from Apple’s Table example (maybe). I’m learning as I go, but I’m still mostly a copy and paste “programmer”.

Which actually brings up another UI question: Are those + - buttons used in places like Network settings available to me to put into my UI?

Maybe I’m not quite understanding what you are looking to accomplish so please check this out.

Thats a very quick project I put together. Inside the zip you will find the demo application and the source files zipped together. Run the application and hit the “+” button. Let me know if thats the functionality you are looking for or if I completely missed the mark on this one.

Holy crap! Wow! Your generosity is remarkable, thank you for putting that together. This is exactly what I’m looking for.

I’ll see if I can get this into my project and I’ll let you know how it goes.

Sounds good. If you need a hand don’t hesitate to ask.

What isn’t entirely clear to me is how I connect a list to the data source. I had been playing with this yesterday as I was trying to understand Apple’s “Table” example and I still can’t make my mind understand this.

Let’s say I have a list defined as {“NO NAME”, “NONAME”}

How does this list get into the data source, then conversely how does the updated data source write back out to the list?

Thank you again.

Okay here are some updated project files for you to look at

In this example when you hit the load button the table is loaded with the values you had in your list. This should give you an idea on how to update it. As for getting values back out of the table/data source and into the list you can do that, but it begs the question why?

I guess I don’t know enough about your project, but the point of having the data source is to have just that… a data source and it’s tied to the table. When you would need the values from your list just access the information in the data source or table directly.

While hard to believe, maybe I don’t understand what I’m doing myself, so I’ll try to explain what I’m doing.

I’m writing a little application that copies an entire disk to a location the user specifies. There are a couple of checkboxes for the user to make decisions about what to do during the process (ie. should the copied files be verified?), a destination path field and a popup menu listing the available disks that the user can choose from to copy. Two things:

  1. I’m saving all of the settings into a preferences file because it is likely the settings won’t change from copy session to copy session. For example the destination may always be the same so it will default to the last destination.

  2. It is also likely that the source disk will always have the same name, or be one of a few names. So I want to present the user this table where they can enter a list of likely candidates (like “NONAME” and “NO NAME”), then when this application wakes up it will by default choose the disk in the popup menu that matches one of these names.

I’m thinking that I will have these disk names stored in an array in the plist file, then what my application opens it reads the values in the array, makes a list and populates the table rows with the list entries. Later the application will update the list with any changes to the table rows then save the list as an array in the plist file to be read back when the application next launches.

Does this explanation help?

I believe I have a grasp on what you’re saying, but if you could upload the project somewhere that would help.

From what I’m following though I would skip the list. At least for the table portion and do the following.

1: When the App launches modify the read_array handler I provided in your other post to populate the Table/Data Source and bypass the list
2: When it’s time to use the data just read it from the table/data source. The data is already in a ‘clean’ format so no need to read it into a list and then be used.
3: When you’re saving the preferences just read straight from the table/data source and write to defaults.

Sounds reasonable, the skipping the list, I suppose I was thinking list because I’ve only ever dealt with lists in my past.

I will clean up my project some and then post it for you to see.

Sounds good, looking forward to seeing it :smiley:

Ok, James, created a project that does kind of where I’m at but without all of the file copying.

So the parts I need to get working are the adding of a new default disk to the stored array in the plist, the retrieval of that array from the plist into the table, and the ability to remove entries from the list.

And of course I need to finesse the UI. :wink: