nstableview custom group row - cocoa

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

Related

NSTableView inside an NSTableViewCell

I'm trying to use an NSTableView inside an NSTableViewCell and have problems disabling scrolling. The outer tableview visualises a list of objects - in my case a 3D material definition - and inside each cell I use (among other controls) another tableview to represent the properties of the material (which is a dynamic list that can change in size).
material A
property 1
property 2
property 3
material B
property 1
material C
...
This works fine except for one thing:
Both tableview's use dynamic row height and this causes the outer table view cell to collapse the inner tableview to a 0px height. Is there a way to disable scrolling for the inner table view and cause it to use enough height to show all elements? What makes it even more complicated is that the properties map to different cell definitions (based on the type of property) and all those cells are setup in Interface Builder. So using an NSView and setting its type to NSTableView to avoid the NSScrollView or doing a programmatic construction of all the controls is not really an option.
Any tips are highly appreciated!
Update: Using TableViews
You can also use NSTableView for what you want do achieve. Unlike UITableView, NSTableView does not do the scrolling itself. It is wrapped inside an NSClipView, which itself is wrapped in an NSScrollView. So you simply extract the tableView out of that and add some constraints.
Interface builder does not support that currently very well. You can't drag the tableView out of its enclosing clipView. But you can open the interface file as source code and remove everything beginning from the scrollView (except the table itself).
The tableView should display fine in the Interface Builder (tested on Xcode 9 and 10)
You have to add the constraints in code, but then the tableView should grow by itself.
Since the inner table view does not have to scroll, you can use just NSStackView to layout your views. Then you don’t have to fight the behaviors of NSTableView.
If you need an example, feel free to ask!

How to change the colour of selected row in NSTableView?

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.

IOS Swift how can stop re-creating cells on drag or scroling in tableview

I am Creating a tableview using custom cell with textfield. When i enter text in tableviewcell textfield and when i scroll the tableview the data is changing . Means re painting cells . How can i stop reloading cell on drag or scroll in IOS 8 swift
Thanks
If your want your cell not re-used, try to make it a subclass of UITableViewCell with a unique identifier, and do not use the identifier with other cells. I haven't test it yet, just hope it will solve your problem.
Ps. If the textfield's text is still overwritten, make a check at the cell's class file (like making an if-else statement checking if the textfield's text is empty).
Detailed workflow:
In cellForRowAtIndexPath(), after you dequeue the cell, normally you will set some property of your custom cell to refresh the data it holds. To implement this, you need to add a didSet observer on the property at the cell's class file. To achieve the goal you want, you can also add the checking code in the didSet observer.
In Your case. You need to customize your keyboard.
Add [Prev][Next] buttons on top of the keyboard and avoid scrolling.
Basically this idea is useful in form based app. May be your doing that kind.
And yes, Stop relaoding of cells is not good to app. If you will implement this. Apple will not approve your app. So avoid this kind of stuffs.
This is how cells work in a UITableView. The standard implementation of the cellForRowAtIndexPath: method dequeues cells and reuses them.
And ideally your app should save the text from the text fields and change the correct text in the cells depending on their indexPath in the same method's implementation.
If you do not want to do that, a dirty workaround would be to create a new cell every time in the cellForRowAtIndexPath: method instead of dequeuing rows.*
*Do not do this unless absolutely necessary. This is very bad coding practice

NSTableBackgroundView

I have a ViewController which has a single TableView within its content.
Within the ViewController I access the actual Table via:
NSView * myView = self.view.subviews[0];
myView = myView.subviews[0];
NSTableView *myTable = myView.subviews[0];
[self.targets sortUsingDescriptors: [myTable sortDescriptors]];
[myTable reloadData];
This actually is clumsy but seems to work. Except that when I mouse down within the table when the vertical scroll bars are active. Then, I raise an exception because in the final line of accessing subviews to reach myTable, the subview array has several NSTableBackgroundViews before the actual TableView that I created and want to access.
NSTableBackgroundView apparently is not documented and does not have a sortDescriptors method (hence the exception).
Perhaps someone can point out a better way to get to the table which is more reliable, or can someone say whether enumerating the final subview array to find the desired ClassOf NSTableView is safe?
Many Thanks!
in lieu of any other answers, I implemented planB, I took the array of subviews and enumerated on them to find the actual NSTableView. This seems to work when I bang on the UI to test it and no more flags are raised.
I wish there were a cleaner way I could discover to get the view I want without traversing all the subviews.

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

Resources