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.
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.
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_
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_
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.
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_
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_
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_
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