different tooltip for each image in NSTableView - objective-c

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.

Related

How can I make command-A select all the NSTextView text in rows in an NSTableView?

So if I have an NSView based tableview and inside the views are NSTextViews which are non-editable but selectable...
how can I get that nice functionality of command-A selects all the text? I don't mean row selection. I have row selection disabled for the tableview. I mean highlighting the text in blue so you can copy it to your clipboard. But not just 1 NSTextView's text from one row, all of them from all the rows.
And in addition to command-A click and drag should do this too. But out of the box it seems I can only select one row's text. Here is video showing problem:
https://dl.dropboxusercontent.com/u/2510380/table.mov
(i keep clicking and dragging but can't highlight text on the next row)
here are two mac apps (skype and gabble) that do this:
https://dl.dropboxusercontent.com/u/2510380/skype.mov
and
https://dl.dropboxusercontent.com/u/2510380/gabble.mov
Assuming they are NOT using WebViews with just HTML inside, how do you get this control over the clipboard? i.e. in Skype you select the text and only the conversation is highlighted, not the timestamp of each message. Also the text copied to the clipboard is formatted very nicely. Can you point me in the right direction to reverse engineer skype?
Unfortunately there's no way to do this easily. This is because only ONE control can be the first responder at a time. This means that, though you can have selection in multiple text views, there are several problems:
Only one text view's text will actually be highlighted with the "live" highlight color; the others will have the gray highlight of non-focused controls.
Copy commands will only apply to the first responder text view.
Drag session starts will be initiated from the control the mouse was actually pointing at (irrespective of first responder) and will only drag that control's text.
In a view-based table view, the controls may not even "exist" for a row not currently displayed, so it'll never get the message unless you forcibly create each row, which could be costly for a large table.
Knowing all this, you might be able to "fake it" by having your controller be complicit in a text view and table view subclass's special handling of a select-all message when it's first responder. On receiving this message, the text view subclass can call super then notify the controller (to get its default behavior AND to let you know it happened), at which point the controller can turn around and send the command to all (existing) text views. Highlighting can be spoofed by overriding the text view's drawing and a drag initiation could defer to a delegate (the controller), which would handle writing ALL the strings from your model to the pasteboard (not even touching the text views in possibly-nonexistent row views). The table view subclass would simply pass the same select-all message to the controller without calling super (and even forcibly making sure nothing is selected before returning for good measure).
I hope this helps. If I've forgotten any of your requirements, let me know.
Try like this:-
First create button programatically then write this code after you create button and also write this code in your load method or awakefromnib method.
NSButton *Buttn=// alloc initwithframe;
[Buttn setKeyEquivalentModifierMask:
NSCommandKeyMask];
[Buttn setKeyEquivalent:#"A"];
[Buttn
setAction:#selector(yourmeth:)];
[Buttn setTarget:self];
// now when you press cmd a write
below code in action method
- (void)selectRowIndexes:(NSIndexSet
*)indexes byExtendingSelection:
(BOOL)extend

Clicking on an image in a collection view

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.

Custom UITableView Cell

What is the Right Way to Implement This?
For example, say I created an XIB file with a button in it. Say a button on that custom controller get pressed, how do the program knows which "row" of the UITableView get pressed? Does each row has their own UITableViewCell Controller?
There has to be one right way to design this.
You can follow the tutorial for custom cells -
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
You are asking about two different controls (buttons and cells), but anyway both have a similar way to "communicate" with the controller. With the button's tag and the cell's index, you have enough to be able to manage the use of these controls.

NSOutlineView elements remain hopelessly undraggable

I have a program with a NSOutlineView (that supports single selection only) from which I'd like to be able to drag elements. These elements should either be received as text or files: for instance, dropping the item on a TextEdit window should put text, but dropping the item on the Finder should create a file. I don't want anything to be dropped over my outline view, even it it comes from itself. This seems easy enough, but for some reason, I can't get it to work.
I checked the NSOutlineView drag and drop example from Apple, and I came to implement the following methods (plus a few definitely unrelated ones):
-(BOOL)outlineView:shouldSelectItem: // I don't expect to drag unselectable items
-(NSArray*)outlineView:namesOfPromisedFilesDroppedAtDestination:forDraggedItems:
-(BOOL)outlineView:writeItems:toPasteboard:
However, when I try to drag an item from my outline view, nothing happens. Instead, it just changes the selection following the cursor.
I've put breakpoints in the two last methods, and they never get called, so their implementation is not the immediate issue.
I must be missing something really obvious here.
Also, this is not (yet) a problem, but how am I supposed to provide contents to my promised files?
I was being stupid and I implemented the methods in the delegate instead of the data source (the two are distinct in my app). Problem solved!
Are you using a custom table view cell? The result of NSCell's hitTestForEvent:inRect:ofView: determines whether a dragging operation can be initiated. It also determines whether your outlineView:writeItems:toPasteboard: should be called.
This method should return NSCellHitContentArea to initiate a drag, or NSCellHitTrackableArea to extend or change the selection.
A standard text cell returns NSCellHitContentArea when you click on the actual text of the cell, and NSCellHitTrackableArea when you click outside of the text. This produces the drag behavior you see in Finder's table view.
You can override this method and always return NSCellHitContentArea if you want all areas of the cell to initiate a drag operation.
See Hit Testing for more information.

Creating NSMatrix of NSImageCells

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.