Search code examples
cocoauitableviewnstableviewnsviewnsscrollview

Is there an intelligent way to handle view swapping/reusing in an NSScrollView?


Background:

I'm building a view-based NSTableView-esque control in Cocoa/Objective-C that offers the ease of use of a UITableView in terms of being able to layout a view in Interface Builder, rather than attempting to subclass NSCell. Its also smart with its resources, in that it adopts a similar reusable cell paradigm like UITableView. This is, of course, all done inside of an NSScrollView.

I've got most of the tricky view swapping figured out, and working. When scrolling, if a view is completely clipped, it is added to a pool of unused views. Similarly, if an undisplayed view comes into the viewable area, it attempts to grab a view from the unused pool, and if none are available, it creates a new one from the nib holding the view prototype.

Similar solutions out there do exist, but I like tinkering around and am enjoying implementing it myself :) . However, I don't know of a solution that handles this issue, and I need some advice!

The issue:

Say, for example, that each view contains a text field. If the text field is being edited, and the user begins to scroll, I'd like for that cell to not be released into the pool so that it doesn't "lose focus", or "give focus" to a newly displayed view that reuses the released one. Is there an intelligent way to mark a view as being edited without requiring the programmer who created the custom view to intercept events, and mark it as such?

Thank you for the assistance!


Solution

  • Your view controller could conform to the NSEditorRegistration informal protocol. It is designed to handle exactly this issue.