Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7441561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:02:07+00:00 2026-05-29T11:02:07+00:00

I’m searching for a way to implement something like reusable cells for UI/NSTableView but

  • 0

I’m searching for a way to implement something like reusable cells for UI/NSTableView but for NSScrollView. Basically I want the same like the WWDC 2011 video “Session 104 – Advanced Scroll View Techniques” but for Mac.

I have several problems realizing this. The first: NSScrollView doesn’t have -layoutSubviews. I tried to use -adjustScroll instead but fail in setting a different contentOffset:

- (NSRect)adjustScroll:(NSRect)proposedVisibleRect {
    if (proposedVisibleRect.origin.x > 600) {
        //  non of them work properly
        // proposedVisibleRect.origin.x = 0;
        // [self setBoundsOrigin:NSZeroPoint];
        // [self setFrameOrigin:NSZeroPoint];
        // [[parentScrollView contentView] scrollPoint:NSZeroPoint];
        // [[parentScrollView contentView] setBoundsOrigin:NSZeroPoint];
    }
    return proposedVisibleRect;
}

The next thing I tried was to set a really huge content view with a width of millions of pixel (which actually works in comparison to iOS!) but now the question is, how to install a reuse-pool?
Is it better to move the subviews while scrolling to a new position or to remove all subviews and insert them again? and how and where should I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:02:08+00:00Added an answer on May 29, 2026 at 11:02 am

    As best I can tell, -adjustScroll: is not where you want to tap into the scrolling events because it doesn’t get called universally. I think -reflectScrolledClipView: is probably a better hookup point.

    I cooked up the following example that should hit the high points of one way to do a view-reusing scroll view. For simplicity, I set the dimensions of the scrollView’s documentView to “huge”, as you suggest, rather than trying to “fake up” the scrolling behavior to look infinite. Obviously drawing the constituent tile views for real is up to you. (In this example I created a dummy view that just fills itself with red with a blue outline to convince myself that everything was working.) It came out like this:

    // For the header file
    @interface SOReuseScrollView : NSScrollView
    @end
    
    // For the implementation file
    @interface SOReuseScrollView () // Private
    
    - (void)p_updateTiles;
    @property (nonatomic, readonly, retain) NSMutableArray* p_reusableViews;
    
    @end
    
    // Just a small diagnosting view to convince myself that this works.
    @interface SODiagnosticView : NSView
    @end
    
    @implementation SOReuseScrollView
    
    @synthesize p_reusableViews = mReusableViews;
    
    - (void)dealloc
    {
        [mReusableViews release];
        [super dealloc];
    }
    
    - (NSMutableArray*)p_reusableViews
    {
        if (nil == mReusableViews)
        {
            mReusableViews = [[NSMutableArray alloc] init];
        }
        return mReusableViews;
    }
    
    - (void)reflectScrolledClipView:(NSClipView *)cView
    {
        [super reflectScrolledClipView: cView];
        [self p_updateTiles];
    }
    
    - (void)p_updateTiles
    {
        // The size of a tile...
        static const NSSize gGranuleSize = {250.0, 250.0};
    
        NSMutableArray* reusableViews = self.p_reusableViews;
        NSRect documentVisibleRect = self.documentVisibleRect;
    
        // Determine the needed tiles for coverage
        const CGFloat xMin = floor(NSMinX(documentVisibleRect) / gGranuleSize.width) * gGranuleSize.width;
        const CGFloat xMax = xMin + (ceil((NSMaxX(documentVisibleRect) - xMin) / gGranuleSize.width) * gGranuleSize.width);
        const CGFloat yMin = floor(NSMinY(documentVisibleRect) / gGranuleSize.height) * gGranuleSize.height;
        const CGFloat yMax = ceil((NSMaxY(documentVisibleRect) - yMin) / gGranuleSize.height) * gGranuleSize.height;
    
        // Figure out the tile frames we would need to get full coverage
        NSMutableSet* neededTileFrames = [NSMutableSet set];
        for (CGFloat x = xMin; x < xMax; x += gGranuleSize.width)
        {
            for (CGFloat y = yMin; y < yMax; y += gGranuleSize.height)
            {
                NSRect rect = NSMakeRect(x, y, gGranuleSize.width, gGranuleSize.height);
                [neededTileFrames addObject: [NSValue valueWithRect: rect]];
            }
        }
    
        // See if we already have subviews that cover these needed frames.
        for (NSView* subview in [[[self.documentView subviews] copy] autorelease])
        {
            NSValue* frameRectVal = [NSValue valueWithRect: subview.frame];
    
            // If we don't need this one any more...
            if (![neededTileFrames containsObject: frameRectVal])
            {
                // Then recycle it...
                [reusableViews addObject: subview];
                [subview removeFromSuperview];
            }
            else
            {
                // Take this frame rect off the To-do list.
                [neededTileFrames removeObject: frameRectVal];
            }
        }
    
        // Add needed tiles from the to-do list
        for (NSValue* neededFrame in neededTileFrames)
        {
            NSView* view = [[[reusableViews lastObject] retain] autorelease];
            [reusableViews removeLastObject];
    
            if (nil == view)
            {
                // Create one if we didnt find a reusable one.
                view = [[[SODiagnosticView alloc] initWithFrame: NSZeroRect] autorelease];
                NSLog(@"Created a view.");
            }
            else 
            {
                NSLog(@"Reused a view.");
            }
    
            // Place it and install it.
            view.frame = [neededFrame rectValue];
            [view setNeedsDisplay: YES];        
            [self.documentView addSubview: view];
        }
    }
    
    @end
    
    @implementation SODiagnosticView
    
    - (void)drawRect:(NSRect)dirtyRect
    {
        // Draw a red tile with a blue border.
        [[NSColor blueColor] set];
        NSRectFill(self.bounds);
    
        [[NSColor redColor] setFill];
        NSRectFill(NSInsetRect(self.bounds, 2,2));    
    }
    
    @end
    

    This worked pretty well as best I could tell. Again, drawing something meaningful in the reused views is where the real work is here.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.