5

再利用可能なセルのようなものを実装する方法を探していますUI/NSTableViewが、NSScrollView。基本的には、WWDC2011のビデオ「Session104-AdvancedScroll ViewTechniques」と同じものが必要ですが、Mac用です。

私はこれを実現するためにいくつかの問題を抱えています。最初:NSScrollViewがありません-layoutSubviews。代わりに使用しようとしまし-adjustScrollたが、別の設定に失敗しました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;
}

次に試したのはwidth、数百万ピクセルの非常に巨大なコンテンツビューを設定することでした(これは実際にはiOSと比較して機能します!)が、問題は、再利用プールをインストールする方法です。
新しい位置にスクロールしながらサブビューを移動するか、すべてのサブビューを削除して再度挿入する方がよいでしょうか。そして、どのように、どこでそれを行うべきですか?

4

1 に答える 1

2

私が知る限り-adjustScroll:、スクロールイベントは普遍的に呼び出されないため、利用したい場所ではありません。-reflectScrolledClipView:おそらくより良いフックアップポイントだと思います。

ビューを再利用するスクロールビューを実行するための1つの方法の要点に当たる次の例を作成しました。簡単にするために、スクロール動作を「偽造」して無限に見えるようにするのではなく、あなたが提案するように、scrollViewのdocumentViewのサイズを「巨大」に設定しました。明らかに、実際の構成タイルビューを描画するのはあなた次第です。(この例では、すべてが機能していることを自分自身に納得させるために、赤と青の輪郭で塗りつぶすダミービューを作成しました。)次のようになりました。

// 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

これは私が知る限りうまく機能しました。繰り返しますが、再利用されたビューで意味のあるものを描くことは、実際の作業がここにあるところです。

お役に立てば幸いです。

于 2012-02-11T16:54:36.273 に答える