How to change the colour of selected row in NSTableView? - macos

I have a view based NSTableView where each row is NSView. When user clicks on any row it should have rounded corner and colour.
I need to achieve something like -
Here trash is selected row.
I found one solution here, But it is NSTableRowView Based solution, not NSView based.
Thanks for your help.

Why is the NSTableRowView solution a problem? In view-based tables, each row consists of two main views:
An NSTableCellView. Since table views can have multiple columns, each row contains an NSTableCellView for each column in the table.
An NSTableRowView. This is displayed behind the NSTableCellViews for each row in the table. By default, this is the view which displays the blue background.
There's a good diagram showing this in Apple's documentation.
As such, creating your own NSTableRowView which draws the red/orange background would be a good way of achieving what you want as it sits behind the NSTableCellViews for the row. You can set these rows up and return them in the - tableView:rowViewForRow: NSTableViewDelegate method.

Related

View based NSTableView and row heights

I'm beginning to try and build an OSX app.
I'm using NSTableView with it set as view-based, however I want the rows to wrap properly (so all of the text in each is shown at once) however I can't seem to find out how do to this.
Using the rowHeight delegate method seems to point to cell-based NSTableViews so I don't know if I'm doing that right or not.
First select NSTableView and then Size Inspector and set row height:
and then select table cell view's which you want change:
And if you want then you can move or resize your table view cell if you want.

View based NSTableView with each view contains 3 labels with should resize based on Text

I need to create a view based tableview in Popover as specified below:
Tableview should be placed in Popover(Popover height should be same as tableview).
Each row should contain a view.
Each row view will contain 3 labels.
Labels should be auto re sizable based on its text height.
Based on 3 labels height, Cell row height should resize.
Based on all cell rows, tableview height should resize.
Based on tableview height, Popover should resize.
I have done this in a static format, but i need to do it in more dynamic format(in future i should be able to add more rows using same classes and methods).
Main problem i am facing is, i am unable calculate the size of cell view in tableView:heigthOfRow: since i don't know the text of labels in this point of time.
So i just created tableview cells in loadView itself and saved in array, and fetching from array in tableview delegate methods. But i think this is wrong way of doing so.
Note: All data to tableview will be given while loading the view itself. Labels are not editable.
Cocoa system resizes the subviews based on superview ,I think scenario that you are looking for is to resize super view based on subview size.
Following 2 solutions i can suggest right awyay,
1.You can choose to post notification upon size change in each subview and make immediate superview observe that.
2.Use globals for size of each subview in your case 3 labels, and have an API to calculate finalRect in your view of view based table view.
Hope this helps:) have a nice day.

nstableview custom group row

I'm creating a view-based table view and I'm having some trouble creating a custom group row. I want to have text in my group row and the default behavior Apple provides is great, but I need to change the color and appearance of the actual row. I have subclassed NSTableRowView and it works, but unfortunately not very well. Specifically when it starts to float I haven't figured out how to change the alpha of the view without making the text alpha change too.
It's actually pretty easy. Make a new subclass of NSTableRowView and override the method drawRect:.
It's best to always call [super drawRect:dirtyRect]; at the beginning.
Then you have the property self.isGroupRow. If it is, then just draw your custom group row.
You can load in the custom Row in the Table View delegate with the method tableView:rowViewForRow:.
Just return an instance of it right there
If I understand your question correctly, this solution should work for you:
Draw a custom NSTableRowView depending on if next row is group row

Cocoa: How to make table view draw custom views in each cell

I am learning Cocoa and trying to create an application for Mac that displays a simple book list. Each book is an NSView with its cover image, title and author. I want to present this list as a NSTableView with a single column and a book view in each cell. However i can't yet figure out how to display a custom view inside a table cell in interface builder or programmatically. Any tips would be very appreciated :)
Inso.
If all of your "book views" are the same size, why not use NSCollectionView / NSCollectionViewItem? It's a much cleaner solution (provided they're all sized the same).
Assuming a collection view wouldn't be a better solution, what you need to do is to write a custom cell. The column owns exactly one such cell, which the table view will use to draw the column's value for each row.
(If you came from the iPhone, yes, this is completely different from UITableView. Each NSTableColumn has exactly one cell, which it uses for every row.)
If you're using your NSView class somewhere else, then you could make it into a subclass of NSControl and have it use another instance of the same cell class. Like most controls, all the real work would be done by the cell, which enables you to reuse that behavior in multiple controls (your single control and your table view).
See Control and Cell Programming Topics for more info.
Apple added view-based table views in Lion, so you should be able do this natively with NSTableView, now.
(You still can't put an NSView in an NSCell—that wouldn't make sense. But you can have views instead of cells in a table view.)

Drop on NSTableView Behavior

I have an NSTableView and I have successfully implemented both tableView:validateDrop:proposedRow:proposedDropOperation: and tableView:acceptDrop:row:dropOperation:.
I don't need tableView:writeRowsWithIndexes:toPasteboard: because that's for dragging objects out of the NSTableView.
Now, the problem is that I want it to behave kind of iTunes-like. In iTunes 9.x (I don't remember it for the previous versions) you have an NSTableView (the playlist) and when you drag a file over it you get this blue focus inside the NSTableView (maybe it's the NSScrollView?) and you don't have the blue horizontal line that indicates where you're going to insert an object. So basically I would like:
No blue horizontal insert line
between rows when hovering a file
over the NSTableView.
The blue focus inside the NSTableView
(or NSScrollView).
Any help would be greatly appreciated so thank you in advance.
From the NSTableView reference:
- (void)setDropRow:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
....
"Passing a value of –1 for row, and NSTableViewDropOn as the operation causes the entire table view to be highlighted rather than a specific row. This is useful if the data displayed by the receiver does not allow the user to drop items at a specific row location"

Resources