Make NSTableView contents of a column conditionally visible?

Hello,

After trying a number of binding combination (which all fail) I wanted to know if it is possible to:

In a NSTableView, make the contents of one column visible if, and only if, its row is selected by user.

I never seen this before, I’m not sure it conforms to Apple’s Interface Guidelines, but it’s only one specific feature of my app, not the general behavior…

Thanks!

Hi,

you can control the drawing of a cell with the
tableView:willDisplayCell:forTableColumn:row:
delegate method

Personally I wouldn’t do that. It looks like a misoperation

Mmmh. I know. Everytime it begins to be difficult to find a method in the docs, it means I’m fighting against the framework. No good.

But how would you do if the user has to guess the contents of a cell before looking at it? Ok, I could add a button like “Reveal solutions” to show them all (making the column visible, for example), but it’s hard for a user to guess, say, 15 solutions before controlling them.

Ok, thank you for your answer. I have been warned. :roll eyes:

Regards,

the normal way to display filtered content is to use predicates like the spotlight search result table view

Ok, so I have to use current application’s NSPredicate to create, say, a isTheLineSelected predicate (which returns a boolean value) and then use

myTableView’s setFilterPredicate_(isTheLineSelected) to activate it, and
setFilterPredicate_(missing value) to clear it. So far, so good.

But does the tableView send a message to the predicate at each cell? where do I actually evaluate the condition? Oh, it does not look simple.

Another way to do this might be to have a blank answer in all the rows of the answer column, like “--------”, and then switch out the dashes for the answer. The following example does this by copying the value of the “ans” field to the “blocked” field when the user selects a row. It also “covers up” the previous answer and shows a new answer when you change to a new row (this can be disabled by commenting out the first and last lines of the tableViewSelectionDidChange: method).

script AppDelegate
	property parent : class "NSObject"
    property theData:missing value
    property lastSel:-1
    
	on applicationWillFinishLaunching_(aNotification)
		setTheData_({{ques:"First Person Singular",ans:"I",blocked:"------"}, ¬
        {ques:"First Person Plural",ans:"We",blocked:"------"} ,¬
        {ques:"Second Person Singular",ans:"You",blocked:"------"} ,¬
        {ques:"Third Person Plural",ans:"They",blocked:"------"}})
        --log theData
	end applicationWillFinishLaunching_
    
    on tableViewSelectionDidChange_(aNote)
        if lastSel is not -1 then theData's objectAtIndex_(lastSel)'s setValue_forKey_("------","blocked")
        set sel to aNote's object's selectedRow()
        theData's objectAtIndex_(sel)'s setValue_forKey_(theData's objectAtIndex_(sel)'s valueForKey_("ans"),"blocked")
        set lastSel to sel
    end
end script

The answer column is bound to array controller.arrangedObjects.blocked, and the script is the delegate for the table view.

Ric

Once again, thanks, Ric.

I used a new “Peeping” column to transfer the data stored into a hidden “Solution” column, giving this:

on tableViewSelectionDidChange_(aNotification)
        set sel to aNotification's object's selectedRow()
        set theData to gVerbFormsController's arrangedObjects
        if selectedLine is not -1 then theData's objectAtIndex_(selectedLine)'s setValue_forKey_("","vPeep")
        set peeping to theData's objectAtIndex_(sel)'s valueforKey_("vSol")
        theData's objectAtIndex_(sel)'s setValue_forKey_(peeping,"vPeep")
        set selectedLine to sel
end tableViewSelectionDidChange_

I had to ask my controller to give back its data, as I use a controller (don’t even know why, in fact). Well, I’m sure there is a much more simple solution, using Applescript record instead of key/value of NSDic.

Anyway, it’s working perfectly, that was exactly what I wanted. I’m glad to have a simple solution, of course maybe I would have learned something about predicates using Stefan’s suggestion.

Thanks again to both of you!

EDIT:

Yes it was.

            if oldSelection is not 0 then set (item oldSelection of gVerbFormsList)'s vPeep to ""
            set newSelection to aNotification's object's selectedRow()+1 -- Applescript is 1-based, Cocoa is 0-based.
            set newLine to item sel of gVerbFormsList
            set newLine's vPeep to newLine's vSol -- could have done a single line but no :)
            set oldSelection to newSelection