macOS cocoa TableView with variable Row-Height and custom cells - xcode

Situation
I want to create a News-Article table within a macOS Cocoa application. (see image below)
Each Cell consists of two labels. One for the header and one for the main-content.
Problem
Now the length of the news-bodies and the width of the whole TableView is variable. I want to have a variable height on each Row/Cell depending on the size of the labels in it.
What we tried
We tried the following:
We have a
TableView
-> TableColumn
-> Custom View
-> LabelHeader
-> LabelBody
The RowSizeStyle Property on TableView is set to "Automatic"
The cells are populated with a custom class. Basically we tried to implement this StackOverflow solution: Link to Solution -> which did not work for us.
(And many more solutions we could not get to run)
Question
Can anyone provide a working tutorial or solution for this problem? Or at least a promising approach?
PS I use XCode 9.4.1 and Swift 4 on macOS 10.13.5
Any help gladly appreciated, Thank you!

In a modern cocoa application you would use NSLayoutConstraint to implement dynamic height of a table view cell. The tricky part would be to get the height of the text that you can set the height of the constrain. This could be done being using an NSTextView instead of NSTextField and asking the layout manager for the current height.
NSRect usedRect = [[textView layoutManager] usedRectForTextContainer:[self textContainer]];
float newHeight = usedRect.size.height;
I used this in an objective c application some years ago so if you have further questions please let me know.

Related

How to change NSTableCellView's height when using storyboards in Interface Builder

When I create a non-storyboard OSX app and add a Table View from the Object Library to my main window in Interface Builder, I am able to change the height of an NSTableCellView inside that Table View.
Doing so, automatically changes the TableView's row height (even when I run the app).
But when I create a storyboard app and follow the exact same steps (adding Table View, changing cellView's height), the TableView's row height does not change, resulting in the NSTableCellView being cropped when I run the app.
I know you can implement the heightForRowAtIndexPath method, but I'm just wondering why this works when using xib files and stopped working when using storyboards. (I really find it a lot easier to design interfaces graphically instead of writing down arbitrary numbers in a text file.)
Is there something I'm missing here? What is the easiest way of doing this, using storyboards?
You have to set the height in the table view (size inspector -> size style: custom and set row height value).

Dynamic TableviewCell heights in Cocoa (Mac)

I am trying to archive a similar behavior to what the Twitter for Mac App has.
It has TableViewCells of dynamic height that properly resize if the user resizes the whole window.
I started using an NSTableView with view based TableViewCells.
Now my problem is that I can calculate the initial height of a TableViewCell, but as soon as the user starts resizing the whole window, the height of course isn't correct any more and the tableview is't asking me again for a new height...
So my question is how I can solve this problem? Are TableViews the way to go?
I'd love it to work with AutoLayout somehow...
Thanks,
Georg

Static UITableView not showing cells with subviews (UIButton, UISlider etc.) in Content View

I've run into a peculiar problem with Xcode. I have a custom UITableViewController that appears as a popover for a few settings in an iPad app. It's a static table view with just 3 cells in 2 sections. It looks fine in the Storyboard editor, but at runtime the cells with custom views (UILabels, UISlider, UIButton) do not show up at all, but those custom views do (in random places).
When I delete the custom elements from the cell or change the cell to anything but custom then they show up fine, even if the view (like basic for example) contains a label in its Content View. It's a lot clearer to see with the attached picture.
To solve this, I've created a completely empty cell below those with custom elements. It looks fine, but I can't interact with any elements. User Interaction is enabled for all elements, cells, and the entire table view. If you have any ideas how to solve that or how to get the cells working properly so I don't need the blank cell hack that'd be much appreciated!
I'm using Xcode 6 beta 7 on OS X Yosemite, programming in Swift.
Thanks in advance!
The question has been answered here Stack Overflow Setting up Auto Layout connections from the label to the Content View solves this issue.
It was a combination of the above link as well as this one that finally solved it.
I added the 4 constraints from each object (UIButton and UISlider) to all four sides of its cell's Content View (top, bottom, leading, trailing). Then, I had to check the "installed" checkbox for each constraint, which was not checked.
Thank you so much for your help, it's working great now!

UITableview bottom is cut off on iphone5

Let me preface this by saying this works perfectly on the iphone 4 - retina and regular.
I have a grouped table that has 4 sections. to set it up i use the dispatch_async stuff in viewWillAppear to grab the information from a server - and then call reloadTable when the server returns.
after the table has been reloaded with the correct information, when i try and scroll down i see more cells but it gives resistance as if i had already reached the end of the table even though there are 3 or 4 more table cells to show and bounces back.
there is one cell that has an image and some text that i use cell.addsubview to add some subviews to it in the cellForRowAtIndexPath function.
but all the other cell heights are normal (44)
I am using IB to have the grouped table view and don't set the frame,bounds, or anything with it in my code - just set the various tablecell heights using the correct function.
does anyone know why it would work perfectly in the iphone 4 (i can scroll down to the bottom and see all the cells) and not in the iphone 5?
Thank you.
I had autolayout checked in the Xib files.
To fix the problem i set self.view.frame.size.height to 568 in the top view controller (i was using a navigation conroller that loaded table views). once i did that, all the child views worked correctly.
I have same problem to fix like below optimized way
if your using autoLayout don't be hardcode just use constraints
i don't know how to use XIB but i always use like this
tableview.frame = cgrectMake(0,320,self.view.frame.size.width,self.view.frame.size.height-64);
64= nabvar height(44)+ top bar (22);
hope this help :)

NSOutlineView for 10.6

I am new to cocoa. I will need to implement an NSoutlineview for 10.6 OS X and above. I have hierarchy of data which must be shown in the form of outlineview. The cell for outline view must contain an image and text. Could some body please suggest on how to go a head with this?
thanks
an outlineview is basically just a tableview so all tutorials for NSTableView apply to it
only the dataSource methods differ
so: http://www.martinkahr.com/2007/05/04/nscell-image-and-text-sample/
and combine it will a tutorial of a cell based outline. e.g. http://devcry.heiho.net/2012/02/treeview-in-cocoa-nsoutlineview.html
For your best practice you can refer this sample code Sourceview
To customize your text you need to set your cell as ImageAndText cell as shown in that samplecode

Resources