Search code examples
swiftcocoanstableviewnsscrollviewnscell

Tracking the position of a NSCell on change


I have a NSTableView and want to track the position of its containing NSCells when the tableView got scrolled by the user.

I couldn’t find anything helpful. Would be great if someone can lead me into the right direction!

EDIT:

Thanks to @Ken Thomases and @Code Different, I just realized that I am using a view-based tableView, using tableView(_ tableView:viewFor tableColumn:row:), which returns a NSView.

However, that NSView is essentially a NSCell.

let cell = myTableView.make(withIdentifier: "customCell", owner: self) as! MyCustomTableCellView // NSTableCellView

So I really hope my initial question wasn’t misleading. I am still searching for a way how to track the position of the individual cells/views.

I set the behaviour of the NSScrollView (which contains the tableView) to Copy on Scroll in IB. enter image description here

But when I check the x and y of the view/cells frame (within viewWillDraw of my MyCustomTableCellView subclass) it remains 0, 0.


Solution

  • I just solved the issue by myself.

    Just set the contents view postsBoundsChangedNotifications to true and added an observer to NotificationCenter for NSViewBoundsDidChange. Works like a charm!