I need to create an NSMatrix with NSImageCells bound to an array controller. So the content of the NSMatrix is bound to an NSArray (so there are as many NSImageCells in the matrix as there are objects in the array), then the image path of the NSImageCells are bound to a key called "iconPath" in the Array Controller.
And then when an image cell is clicked in the matrix, I need it to perform an action. I know how to use bindings, and I have a concept like this working in an NSTableView, but I have never used NSMatrix before, so are there any good resources or sample code that would help me get started with this?
Thanks
If you want them to send actions when you click on them, then you probably want NSButtonCell instead of NSImageCell. A button cell can still contain an image.
Related
Setup
I have a NSCollectionView. I have a checkbox in the View Prototype. I've successfully set up bindings so the Card Title and action get populated. (image 1, below)
Goal
I'd like, when I click the checkbox, to run a function that accesses the specific CardModel that the View Prototype is already able to access. I'll then manipulate its data accordingly.
Research
I found this article on SO: Get the representedObject values of NSCollectionViewItem NSButton click, which describes my situation pretty well. The answer, unfortunately, is without specific code. Here's what's suggested:
So, first, set the represented object of your button's cell to the
collection view item that owns the button. (You can do this in the nib
editor.) Then, in your action method, get the button's cell, then the
cell's represented object (which is the item), then the item's
represented object.
Seems simple enough, right?
Attempted Solution(s)
I create a method cardCheckBoxClicked: and connect it to the checkbox.
As per the advice above, I connect the button cell's outlet representedObject to Card Collection View Item. (image 2)
I then attempt to get the Card Collection View Item's representedObject in code.
From MainWindowController.h:
-(IBAction)cardCheckBoxClicked:(id)sender
{
CardModel* cModel = [[sender representedObject] representedObject];
NSLog(#"card title: %#",cModel.title);
}
Error
When I click on the checkbox, I get the following error:
-[NSButton representedObject]: unrecognized selector sent to instance 0x6080001581b0
Question!
So - how do I access the button cell's represented object? Did I misunderstand the advice given above? How can I successfully access the data I need?
Images (reference)
bindings example
represented object connection
This here:
-[NSButton representedObject]:
Is you asking the Class NSButton to run the method representedObject. Make sure you distinguish between a Class an an object or instance of that class.
You need to take the actual button, get its button cell, (at least I think that's what you want), and then call representedObject on the cell. If I am understanding you correctly. I never touch interface builder, so here's completely made up code that lines up with what you are asking for.
someObject = [[theButton cell] representedObject];
In addition to CH Buckingham's answer, you should also consider using bindings. You can bind the checkbox's value binding to the collection view item with a model key path which goes through representedObject to some property on your CardModel. (If desired, the key path can keep going through your model object graph.) That will set that property whenever the button is toggled.
I have a collection view and each item has an image and a label. I want to click the NSCollectionViewItem or NSImage and then hide the collection view and display a completely separate view containing the details of the object that was clicked.
I cant find any documentation on how to handle click events in this situation. How is this possible? I have built out the collection view in Interface Builder so everything was done via bindings as opposed to code.
#Jeff, I don't have permissions to add a comment so writing this as answer.
You can overwrite setSelection in your subclass of NSCollectionViewItem (as explained by #indragie in Selection Highlight in NSCollectionView) to track the selected item and perform an action.
The solution that I went with was to not actually use an Image Well, aka NSImage. I used a button and bound the Image property to an instance of an NSImage that I exposed as a property on my model.
It was easy enough but I'm shocked more people havent been asking this question.
I have a NSTableViewwith a NSImageCellin it. I would like to add a tooltip that when I hover the image with the mouse i get a tooltip with the Path of the image. What is the best way to do this?
the problems I have are these:
retrive the path of the hovered image
display it in the tooltip
please keep your answers easy, I am a newbie.
---EDIT---
additional info:
myarray.arrangedobjects is what fills the table. it is an array of NSMutableDictionaries. the dictionary has following Keys:
image (the one displayed in the table that i would like to place the tooltip over)
imagepath (the text i would like to display in the tooltip)
filename (is displayed in another cell)
You will need to provide a delegate for the NSTableView and implement this delegate method:
– tableView:toolTipForCell:rect:tableColumn:row:mouseLocation:
You can find the docs for the NSTableViewDelegate protocol here. You need to make sure to plug the delegate into the delegate outlet on the NSTableView using IB. Your method will not be called unless that has been done. Also, it's worth noting that this delegate method is called on demand, not up-front, so it wont get called until you've parked your mouse hovering over a cell. This approach should work for both tables with a data source and tables that use bindings.
I put a trivial example up on GitHub.
I have a NSTableView and each row has a NSComboBox.
The table column is bounded to NSArrayControllerA and each NSComboBox is bounded to NSArrayControllerB.
I would like to be notified when the selected value in any NSComboBox changes.
So far, I've tried to add a listener to the NSArray managed by the NSArrayControllerB, but I'm not notified about any change.
I've also tried to add an observer to the NSTableView, with a NSComboBoxSelectionDidChangeNotification but it seems the notifications are not propagated to the parent views. And the combo boxes are dynamically created when a new row is inserted.
thanks
UPDATE: How is the NSComboBox bound ?
OK. I assume you've bound the combo box's selection to the array controller's selection? If so, try observing the array controller's selectionIndex property. (It's KVO-compliant.)
On my iPhone so I can't easily test right now, but that should do the trick.
OK, scratch that, now that I better understand the question.
How about instead you set the selector for the cell to some method in your controller (with an outlet to the enclosing table view), say, -comboBoxClicked: and then implement something like:
- (void)comboBoxClicked:(id)sender
{
NSUInteger changedRow = [[self tableView] selectedRow];
// Do something with changedRow
}
I did a cursory test (just NSLogging changedRow) and it seemed to work for me, at least in a very basic application.
Does anyone have any info on this? I'm new to cocoa, all tutorials seem to be for iPhone which uses a different view controller. Anyone willing to provide a step by step for adding labels to a custom cell? (I'm pulling from Core Data)
EDIT: It's important to note I'm using Core Data here. Many tutorials use arrays.. I don't understand why you would use that??
I'll award an answer quickly!
Zach
I'm not sure if this is going to help you, but your problem seems unrelated to your use of CoreData.
If I've understood your problem correctly, here are some steps:
Populate your NSTableView
Using CoreData, what you can do is put an NSArrayController object in your XIB document, set its mode to Entity and choose the Entity you want to display in your TableView (all of this from the first tab of the inspector on your array controller object).
Then, bind your NSTableView Content to the arrangedObjects of the array controller. You might also bind the selectionIndexes and use some sort descriptors on the array controller to order your data, as CoreData will give you a set rather than an array.
Click on your table view as many times as necessary as to select the table column in which you want your custom cell to appear, and set its Value binding to arrangedObjects too.
Set your custom cell
Finally, click on the cell of this table column and in the "Identity" of the inspector, change the class name of the cell to the class name of your custom cell.
I'll let you read the appropriate documentation to implement your custom cell according to what you want to achieve.
With the different bindings I've described, the objectValue of your cell should be the object from your array controller at the same index of the row your cell will appear on.
Please note that I've not tested again all of these steps but answered from memory... there might be details I've missed but you should have the main steps here.