Wednesday, November 26, 2014

#1 2013-01-03 05:46:56 pm

CarbonQuark
Member
Registered: 2005-08-25
Posts: 217

Table View Application

Hello all,

I'm updating an old AppleScript based Xcode 2 project to Xcode 4 and I have run into a road block on how to setup a table view and connect a data source and then read/write data to file with add/update/delete buttons to modify the records.

There is an existing tutorial that has everything that I need here:
http://macscripter.net/viewtopic.php?id=30359

However, it is outdated.

Does anyone have a sample project or link to a tutorial that covers this using Xcode 4?

Any information would be greatly appreciated.

Thank you,
Mike

Offline

 

#2 2013-01-03 08:01:47 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3813

Re: Table View Application

For some reason the linked version seems to have left out some coercions in tableView_objectValueForTableColumn_row_. Try changing it to this:

Applescript:

   on tableView_objectValueForTableColumn_row_(aTableView, aColumn, aRow)
       if theDataSource's |count|() as integer is equal to 0 then return
       set ident to aColumn's identifier()
       set theRecord to theDataSource's objectAtIndex_(aRow)
       set theValue to theRecord's objectForKey_(ident)
       if ident's isEqualToString_("theStatus") as boolean then
           if theValue's intValue() as integer = 0 then
               set theValue to NSImage's imageNamed_("green")
           else
               set theValue to NSImage's imageNamed_("red")
           end if
       end if
       return theValue
   end tableView_objectValueForTableColumn_row_


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2013-01-04 08:12:57 am

CarbonQuark
Member
Registered: 2005-08-25
Posts: 217

Re: Table View Application

Hi Shane,

Thanks for the reply. I'll try my best to get that to work.

I'm still trying to figure out how to get everything to link up. Things are different in Xcode 4.

I remember Apple having example files as part of the developer tools release. Table view was one of the examples. It had buttons for add, remove and update records.

I really wish then had a simple source file for this.

Thank you,
CarbonQuark

Offline

 

#4 2013-01-04 11:51:33 am

CarbonQuark
Member
Registered: 2005-08-25
Posts: 217

Re: Table View Application

Hey All,

The thing that is tripping me up the most is how to connect the table view to a data source?

I downloaded the outdated source in Craig's tutorial and inspected the AppleScript and IB layout and I'm not seeing how the connection is made.
http://macscripter.net/viewtopic.php?id=30359

Does anyone have any idea on how this happens? I know it's probably something simple. I have tried different connections and can't seem to get it. All of the Xcode 4 tutorials on table views/data source are targeting iOS so they haven't been much help.

Thanks,
CQ

Last edited by CarbonQuark (2013-01-04 12:10:05 pm)

Offline

 

#5 2013-01-04 04:14:33 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3813

Re: Table View Application

You control-click on the table view, then in the HUD panel that appears you drag from the circle next to datasource across to the blue cube representing your app delegate instance.

I remember Apple having example files as part of the developer tools release.

FWIW, my book (see my sig below) comes with many example projects.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#6 2013-01-04 04:41:06 pm

CarbonQuark
Member
Registered: 2005-08-25
Posts: 217

Re: Table View Application

Hi Shane,

Thanks again.

I'll check out your book. What version of Xcode are your example files built with? I'm using Xcode 4.5.2

I had already tried what you suggested.

When creating a test project I followed Craig's video tutorial which was great.
Craig's video:
http://allancraig.net/index.php?option= … ;Itemid=90

Here is my code (modified from Craig's):

Applescript:


-- Application Properties
   property timeTrackerTableView : missing value
   property timeTrackerWindow : missing value
   property jobDateTextField : missing value
   property jobNumberTextField : missing value
   property jobHoursTextField : missing value
   
-- Application Bindings
   property jobDate : ""
   property jobNumber : ""
   property jobHours : ""

-- Data Properties
   property theDataSource : {}

-- Application Methods
on awakeFromNib()
set theDataSource to NSMutableArray's alloc()'s init()
set theData to {{jobDate:"123", jobNumber:"123", jobHours:"123"}, {jobDate:"123", jobNumber:"123", jobHours:"123"}}
theDataSource's addObjectsFromArray_(theData)
timeTrackerTableView's reloadData()
end awakeFromNib

I dont understand how "dataSource" which is connected to my app delegate connects to the property "theDataSource"

In Craig's example I couldn't find the connection.

When I run the program the Table View doesn't display anything


I'm also getting this error:
*** Illegal NSTableView data source.  Must implement numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:

Offline

 

#7 2013-01-04 04:48:36 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3813

Re: Table View Application

CarbonQuark wrote:

What version of Xcode are your example files built with?

4.5.2. The table examples are built using Cocoa bindings rather than datasources (which is simpler in most cases).

I dont understand how "dataSource" which is connected to my app delegate connects to the property "theDataSource"

It doesn't. theDataSource is just a variable that the data is stored in, and that the datasource handlers provide to the table.

*** Illegal NSTableView data source.  Must implement numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:

That means those datasource handlers aren't being found in your script.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#8 2013-01-07 01:37:17 pm

CarbonQuark
Member
Registered: 2005-08-25
Posts: 217

Re: Table View Application

Hi Shane,

I just purchased your book and it looks like it has pretty much everything I need!

Thank you,
CQ

Offline

 

#9 2013-01-07 04:08:33 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3813

Re: Table View Application

Thanks -- and good luck!


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#10 2013-01-07 07:01:15 pm

CarbonQuark
Member
Registered: 2005-08-25
Posts: 217

Re: Table View Application

Hi Shane,

Still working with your examples. Your examples compile correctly. But when I follow your steps in the book and build from scratch I can't seem to get things to work.

I'm trying to build an app that has the exact functionality as Craig's tutorial. Table View and text fields for entry that allow the user to add, update and remove from the Table View... and a way to load and save data to a file.

Very frustrating.

I'm sure it's something simple.

Back to the drawing board.

CQ

Offline

 

#11 2013-01-07 07:08:34 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3813

Re: Table View Application

CarbonQuark wrote:

Still working with your examples. Your examples compile correctly. But when I follow your steps in the book and build from scratch I can't seem to get things to work.

Email me a non-working project.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#12 2013-01-07 08:40:56 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3813

Re: Table View Application

For anyone lurking, the problem was not having any memory management.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#13 2013-01-09 03:12:49 pm

CarbonQuark
Member
Registered: 2005-08-25
Posts: 217

Re: Table View Application

Hi Shane,

I'm still trying to figure out the most elegant way to update an item within the data array.

I'm using this to add

Applescript:

tell theArrayController to addObjects_({{itemOne:itemOne, itemTwo:itemTwo, itemThree: itemThree}})

I'm simply using remove: action of the Array Controller to remove.

What is the best way to update an item in the array?

Also, I selected "Auto Rearrange Content" in the array controller which works great when the user selects a column to sort on. How do I setup a column to auto rearrange by default?

Any information would be greatly appreciated,
CQ

Offline

 

#14 2013-01-09 03:20:55 pm

CarbonQuark
Member
Registered: 2005-08-25
Posts: 217

Re: Table View Application

When I try changing theData which is bound to the array controller it doesn't automatically update the view. You have to click off or on the view to see the change.

Is it better to change the array controller data instead? If so, how?

Offline

 

#15 2013-01-09 04:11:34 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3813

Re: Table View Application

You'll find it simpler to modify your data via the array controller. There are a range of methods: add:, addObject:, addObjects:, insertObject:atArrangedObjectIndex:, etc.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#16 2013-01-09 04:41:03 pm

CarbonQuark
Member
Registered: 2005-08-25
Posts: 217

Re: Table View Application

I had found that list of methods and insertObject:atArrangedObjectIndex: seems like the one. However I couldn't figure out to implement it. I kept getting errors.

Still trying to figure out the sort. I have it working where I update the data and tell the view to update. Is there a way that the controller could sort the data automatically?

Offline

 

#17 2013-01-09 06:04:01 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3813

Re: Table View Application

CarbonQuark wrote:

I had found that list of methods and insertObject:atArrangedObjectIndex: seems like the one. However I couldn't figure out to implement it. I kept getting errors.

Post some code.

Is there a way that the controller could sort the data automatically?

You have to trigger it. Try rearrangeObjects.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#18 2013-01-09 06:23:44 pm

CarbonQuark
Member
Registered: 2005-08-25
Posts: 217

Re: Table View Application

Hi Shane,

Thanks for the reply!

I found out how to use insertObject:atArrangedObjectIndex: in your book.

I explored two different ways to update...

With this option when you update the table row the row stays selected

Applescript:


theData's replaceObjectAtIndex_withObject_(myTableView's selectedRow as integer, {itemOne:itemOne, itemTwo:itemTwo, itemThree: itemThree})
myTableView's reloadData()

With this option when you update the table row the row doesn't stays selected (which I need)

Applescript:


tell theArrayController to removeObjectAtArrangedObjectIndex_(myTableView's selectedRow as integer)
tell theArrayController to insertObject_atArrangedObjectIndex_({itemOne:itemOne, itemTwo:itemTwo, itemThree: itemThree}, myTableView's selectedRow as integer)

Trying to find rearrangeObjects in the controller now for sort.

I did find this code which works (but only if a sort column is selected by user)

Applescript:


set sortDesc to myTableView's sortDescriptors()
theData's sortUsingDescriptors_(sortDesc)
myTableView's reloadData()

I'm trying to sort the array by the key "itemOne" every time a new entry is added to the array.

Offline

 

#19 2013-01-09 06:35:23 pm

CarbonQuark
Member
Registered: 2005-08-25
Posts: 217

Re: Table View Application

You have to trigger it. Try rearrangeObjects.

I can't figure that one out. I found a method in your book that sorts lists but it doesn't work with either theArrayController or theData

Offline

 

#20 2013-01-09 09:31:26 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3813

Re: Table View Application

I wonder whether it might be worth rethinking your UI a bit.

The simplest table involves just editing values in place, but sometimes you need them in separate fields. But if you bind the contents of those fields to the array controller with a Controller Key of selection and a suitable Model Key Path, you get the benefit of a separate entry area but without the need for lots of code. You just click on an entry, and the values appear in the fields; edit the fields, and the table updates.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#21 2013-01-09 10:08:13 pm

CarbonQuark
Member
Registered: 2005-08-25
Posts: 217

Re: Table View Application

Hi Shane,

Thanks again for the reply.

That's an interesting idea I'll have to play with that to find out how a new entry would be created.

I'm still trying to figure this sort thing out.

Is there a way to do the following without the user having to click on the column header?

Applescript:

set sortDesc to myTableView's sortDescriptors()
theData's sortUsingDescriptors_(sortDesc)
myTableView's reloadData()

Thanks,
CQ

Offline

 

#22 2013-01-09 11:09:29 pm

CarbonQuark
Member
Registered: 2005-08-25
Posts: 217

Re: Table View Application

New issue: If I sort the table by clicking the table column header and use the code below the selectedRow index doesn't match up with the what's in theData.

Applescript:


set my itemOne to itemOne of item ((myTableView's selectedRow as integer)+1) of theData

Offline

 

#23 2013-01-09 11:16:35 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3813

Re: Table View Application

CarbonQuark wrote:

New issue: If I sort the table by clicking the table column header and use the code below the selectedRow index doesn't match up with the what's in theData.

That's right. The raw data and what's showing in the table don't match in order. Get the data directly from the array controller (selectedObjects()).


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#24 2013-01-09 11:31:32 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3813

Re: Table View Application

CarbonQuark wrote:

Also, I selected "Auto Rearrange Content" in the array controller which works great when the user selects a column to sort on. How do I setup a column to auto rearrange by default?

Clicking on a column header sets the array controller's sortDescriptors property. Until you do that, the controller has no sortDescriptors -- unless you add them using setSortDescriptors_, passing an array of NSSortDescriptors.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#25 2013-01-10 12:23:25 am

CarbonQuark
Member
Registered: 2005-08-25
Posts: 217

Re: Table View Application

Shane,

Thanks! I'll give that a try.

Also, your book is great! It has been so helpful!

CQ

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.034 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)