• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Auto-sized table cells, for macOS
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Auto-sized table cells, for macOS


  • Subject: Re: Auto-sized table cells, for macOS
  • From: Quincey Morris <email@hidden>
  • Date: Tue, 14 Mar 2017 20:33:34 -0700

On Mar 14, 2017, at 18:26 , Daryle Walker <email@hidden> wrote:
>
> You’d think that this would be a solved problem….

It sort of is. I think you can find a solution on stackoverflow.com <http://stackoverflow.com/> (which is where I got the idea from IIRC) but you have to wade through the out of date stuff to pick out a modern way of doing it. Here’s the code I came up with (for an outline view, table view should be similar):

> 	func resizeListItem (_ listItem: RecentObjectValue, cell: NSTableCellView, width: CGFloat, resizedRows: NSMutableIndexSet? = nil)
> 	{
> 		cell.objectValue = listItem
>
> 		cell.textField?.preferredMaxLayoutWidth = width - 2
> 		cell.needsUpdateConstraints = true
> 		cell.layoutSubtreeIfNeeded ()
>
> 		let rowHeight = cell.fittingSize.height
>
> 		guard listItem.rowHeight != rowHeight else { return }
>
> 		listItem.rowHeight = rowHeight
> 		let row = recentsListView.row (forItem: listItem)
>
> 		guard row >= 0, let rows = resizedRows else { return }
>
> 		rows.add (row)
> 	}

The listItem parameter is a custom struct that contains the actual data displayed in the row (the data model of the outline view, in effect), but it also caches the row height. The resizedRows parameter just accumulates an optional set of changed heights for a subsequent “noteHeightOfRowsChanged”. The cell parameter is created once for measuring purposes, in viewDidLoad:

> 		/* view controller instance variable */ headerCell = recentsListView.make (withIdentifier: "HeaderCell", owner: self) as! NSTableCellView
> 		let headerConstraint = NSLayoutConstraint (item: headerCell, attribute: .height, relatedBy: .greaterThanOrEqual, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 17)
> 		NSLayoutConstraint.activate ([headerConstraint])

The only other thing you need is a strategy for deciding when to recalculate the heights. When the column width changes, obviously, as well as for inserted rows, but there might be other cases as well.
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Auto-sized table cells, for macOS
      • From: Daryle Walker <email@hidden>
References: 
 >Auto-sized table cells, for macOS (From: Daryle Walker <email@hidden>)

  • Prev by Date: Re: Getting the true rotation of a UIView layer
  • Next by Date: Re: Getting the true rotation of a UIView layer
  • Previous by thread: Auto-sized table cells, for macOS
  • Next by thread: Re: Auto-sized table cells, for macOS
  • Index(es):
    • Date
    • Thread