Tableview help

Hi guys,

Thanks for your help, my app is going great but i’ve hit another little stumbling block & need so guidance.

Basically, is there a recent tutorial on using a Tableview with Xcode 5?

Below is an example of the apps GUI, people should be able to click the + button & add items to the table views above, highlight a view & press the minus to delete where needed.

Some pointing in the right direction would be great

A large slice of my book is about using tables. You need to be aware, though, that as of 10.7, tables can be view-based instead of cell-based. View-based is the way of the future, but it’s also more complicated. My book only covers cell-based tables. Whatever, you need to keep the distinction in mind when reading any documentation – the bindings for the two are quire different.

Basically you’re easiest bet is to use an array controller connected between a property containing the data and the table. Then the + and - buttons trigger actions on the array controller.

Just grabbed “AppleScriptObjC Explored v5”

Will report back, many thanks!

Hi Shane,

I think the example from “AppleScriptObjC Explored v5” chapter 6 is almost what i’m looking for, what i’m trying to do is prompt the user to select a .pkg then add that to the array.

This is an example, which is triggered by a new button i’ve added:

    on selectPKG_(sender)
        try
            choose file of type {"com.apple.installer-package-archive"} with prompt "Select a .pkg to add:" default location (path to desktop folder)
            
            -- Get path of the selected app.
            set my selectedPKGPath to POSIX path of result
            
            -- Log path of the selected app
            log selectedPKGPath
            
            set my theData to selectedPKGPath
        end try
        
    end selectPKG_

But trying to add this one .pkg’s path is failing with:

Any pointers?

Once I have the above, I think it will be easy enough to do what i’m trying next.

So i have amended the above, now no errors…

But how to update the array in the tableview xib?

    
on selectPKG_(sender)
        try
            choose file of type {"com.apple.installer-package-archive"} with prompt "Select a .pkg to add:" default location (path to desktop folder)
            
            -- Get path of the selected app.
            set my selectedPKGPath to POSIX path of result
            
            -- Log path of the selected app
            log selectedPKGPath
            
            set my theData to {{firstName:selectedPKGPath}}
            
            log theData
            
        end try
        
    end selectPKG_

This seems to work:

    on selectPKG_(sender)
        try
            choose file of type {"com.apple.installer-package-archive"} with prompt "Select a .pkg to add:" default location (path to desktop folder)
            
            -- Get path of the selected app.
            set my selectedPKGPath to POSIX path of result
            
            -- Log path of the selected app
            log selectedPKGPath
            
            set my theData to theData & {{firstName:selectedPKGPath}}
            
            log theData
            
            theData's reloadData()

        end try
        
    end selectPKG_

now to write to a plist & read from

You shouldn’t need that if you’re using an array controller.

Thanks Shane.

I’ve removed that.

I’m seeing an issue where removing an item from the array, (with a button bound to the array controllers remove action), stops the array from being updated.

What happens if you change the “set my theData to…” to use an NSMutableArray rather than a list?

Is that when declaring the myData property?

Tbh, I’m wondering if all I want is a list with these paths written to a plist, is an array controller needed? It’s a simple list.

You need the array controller for the table, if I’m following correctly.

What I mean is changing this:

set my theData to theData & {{firstName:selectedPKGPath}}

to:

set my theData to current application's NSMutableArray's arrayWitharray:( theData & {{firstName:selectedPKGPath}})

+[NSMutableArray arrayWitharray:]: unrecognized selector sent to class 0x7fff72c9a818

huh

slightly wrong camel casing

. arrayWithArray .

Thanks again, do i need to change my TableViews binding?

    

on selectPKG_(sender)
        try
            choose file of type {"com.apple.installer-package-archive"} with prompt "Select a .pkg to add:" default location (path to desktop folder) with multiple selections allowed
            
            -- Get path of the selected app.
            set my selectedPKGPath to POSIX path of result
            
            -- Log path of the selected app
            log selectedPKGPath
           
            --set my theData to theData & {{certs:selectedPKGPath}}
            
            set my theData to current application's NSMutableArray's arrayWithArray:(theData & {certs:selectedPKGPath})
            
            log theData
            
            tell standardUserDefaults() of current application's NSUserDefaults
                
                setObject_forKey_(theData, "theData") as list
            
            end tell
            
            log "updated plist"
            
        end try
        
    end selectPKG_


If I uncomment:

& comment the line below it… it works.

I doubt that it works at all.

choose file with multiple selections allowed returns a list of alias specifiers,
so POSIX path of a list will throw an error when multiple files are selected.

I recommend either to coerce the list one by one or use Cocoa’s NSOpenPanel


choose file of type {"com.apple.installer-package-archive"} with prompt "Select a .pkg to add:" default location (path to desktop folder) with multiple selections allowed
set thePaths to result
-- Get path of the selected app.
set pathList to {}
repeat with aPath in thePaths
	set end of pathList to POSIX path of aPath
end repeat
set my selectedPKGPath to pathList


StefanK, thanks for that… I did the cardinal sin of testing to many bits at the same time & not reverting.

That also solved an issue when writing to the plist, as now it shows like:

& not the below in the plist:

Which was going to be my next question.

However, by running the below… the tableview is not being populated correctly. It’s only showing a (

Any ideas?

    on selectPKG_(sender)
        try
            choose file of type {"com.apple.installer-package-archive"} with prompt "Select a .pkg to add:" default location (path to desktop folder) with multiple selections allowed
            
            set thePaths to result
            -- Get path of the selected app.
            set pathList to {}
            repeat with aPath in thePaths
                set end of pathList to POSIX path of aPath
            end repeat
            set my selectedPKGPath to pathList
           
            set my theData to theData & {{certs:selectedPKGPath}}
            
            log theData
            
            tell standardUserDefaults() of current application's NSUserDefaults
                
                setObject_forKey_(theData, "theData") as list
            
            end tell
            
            log "updated plist"
            
        end try
        
    end selectPKG_

nvm, figured out how to change the array format.

The below shows that, what should the value binding in the tableview be now?

arrangedObjects & certs is what is was.

    on selectPKG_(sender)
        try
            choose file of type {"com.apple.installer-package-archive"} with prompt "Select a .pkg to add:" default location (path to desktop folder) with multiple selections allowed
            
            set thePaths to result
            -- Get path of the selected app.
            set pathList to {}
            repeat with aPath in thePaths
                set end of pathList to POSIX path of aPath
            end repeat
            
            set my selectedPKGPath to pathList
            
            log selectedPKGPath
           
            set my theData to theData & selectedPKGPath
            
            log theData
            
            tell standardUserDefaults() of current application's NSUserDefaults
                
                setObject_forKey_(theData, "theData") as list
            
            end tell
            
            log "updated plist"
            
        end try
        
    end selectPKG_

Sorry, but I’ve got it… (should preview posts before submitting as always seem to work after posting).

The binding has now been changed to: arrangedObjects with nothing in the model key path.

Right, now time to figure out how to read the arrays value from a plist.

Thanks both!

ok… same for checking it all before posting.

Still seeing an issue where removing an item from the array, (with a button bound to the array controllers remove action), stops the array from being updated.

I am now removing using the following, this updates theData & the plist…

    on deletePKG_(sender)
    
        tell theArrayController to set my selectedRow to selectionIndex()
        
        log selectedRow
        
        tell theArrayController to removeObjectAtArrangedObjectIndex:selectedRow
        
        log "done"
        
        log theData
        
        tell standardUserDefaults() of current application's NSUserDefaults
            
            setObject_forKey_(theData, "theData") --as list
            
        end tell
        
        log "updated plist"
    
    end deletePKG_

But i still cannot add new items to the array… The below doesn’t seem to be adding the newly selected items to the array.

set my theData to theData & selectedPKGPath

In fact, it seems to be replacing theData with the value of selectedPKGPath