- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在寻找一种方法来为 UI/NSTableView
但为 NSScrollView
实现可重用单元格之类的东西。基本上我想要与 WWDC 2011 视频“Session 104 - Advanced Scroll View Techniques”相同但适用于 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;
}
我尝试的下一件事是设置一个非常大的内容 View ,宽度
为数百万像素(与 iOS 相比,这确实有效!)但现在的问题是,如何安装一个重用池?
在滚动到新位置时移动 subview 或删除所有 subview 并再次插入它们更好吗?我应该如何以及在哪里做?
最佳答案
据我所知,-adjustScroll:
不是您想要利用滚动事件的地方,因为它不会被普遍调用。我认为 -reflectScrolledClipView:
可能是一个更好的连接点。
我编写了以下示例,该示例应该达到了执行 View 重用 ScrollView 的一种方式的要点。为简单起见,我将 scrollView 的 documentView 的尺寸设置为“巨大”,正如您所建议的,而不是试图“伪造”滚动行为以使其看起来无限。显然,真正绘制构成图 block View 取决于您。 (在这个例子中,我创建了一个虚拟 View ,它只用红色和蓝色轮廓填充自己,以说服自己一切正常。)结果是这样的:
// 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
据我所知,这非常有效。同样,在重用的 View 中绘制一些有意义的东西才是真正的工作所在。
希望对您有所帮助。
关于objective-c - NSScrollView 无限/无限滚动 | subview 重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9115944/
我有一个 NSScrollview 嵌套在另一个 NSScrollview 中。如何使内部 View 仅处理水平滚动?垂直滚动应该移动外部 View 。 目前我将 scrollWheel: 事件从内部
有谁知道将 NSScrollView 滚动到顶部的正确方法?我正在寻找与 UIView 的 scrollToTop 方法等效的方法。 到目前为止,这是我所拥有的,但在所有情况下都不完全正确 [self
我阅读了文档 Synchronizing Scroll Views ,并且完全按照文档进行,但是有一个问题。 我想同步一个 NSTableView 和一个 NSTextView。先让NSTableVi
当您轻轻滚动 NSScrollView 时,Cocoa 标记为脏的矩形并传递给 drawRect,通常很小(可能是对于垂直 ScrollView ,高度小至一或两个像素)。框架显然已经知道大部分内容是
我有一个 NSScrollview 其中包含两个 View ,让我们分别将它们称为View A 和View B,现在它们都可以增长高度,我希望 View A 出现在顶部和底部的 View B ,当两者
我在 Mac 上使用简单的 NSScrollView 时遇到了一个奇怪的问题。我刚刚开始使用 Cocoa 编程,但我之前有过在 iOS 上使用 UIKit 的经验。我将 NSScrollView 添加
我正在尝试将链接属性添加到 NSTextView 的 NSMutableString。 由于我只想使用 TextView 将链接添加到可见文本,因此我通过 的 NSViewBoundsDidChang
我打算在 NSScrollView 中使用 NSOutlineView,它不会紧贴顶部和底部边缘,因为我需要在顶部和底部滚动边缘插入阴影(重叠渐变)。轮廓 View 应该可以滚动到垂直顶部阴影下方和底
我的主窗口中有一个 NSScrollView 项目。每当我启动该程序时, ScrollView 中都会有文本,并且它从中心开始。用户不应该从顶部开始阅读,但为什么应用程序要在中心启动它?谢谢! 最佳答
我有一个 NSCollectionView,它列出了核心数据源中的项目。每个项目都有一个按钮,触发时会滑下操作 View 。当此 View 向下滑动时, Collection View 会缩小,以便两
我有一个 NSScrollView ,我向其中添加了一堆具有 NSImage subview 的 NSViews 。基本上它是 ScrollView 中包含的一长排缩略图。 一切都很好......直到
NSScrollView 在 Mac 应用程序中如何工作?我编写了以下代码,但滚动不起作用。 NSDictionary *temp=[[ NSDictionary alloc] init ]; NS
我正在尝试构建一个无尽的 NSScrollView,即可以在任一方向无限滚动的 ScrollView 。这是通过具有固定尺寸的 ScrollView 来实现的,一旦它太靠近任一边缘,该 ScrollV
我正在尝试使用 Lion 中的 NSScrollView 构建类似于在 iPhone 应用程序中如此流行的拉动刷新概念的东西,但是没有像 contentOffset 这样的属性,并且当我滚动到上面时,
我有一个 Cocoa 窗口,其内容 View 包含一个 NSScrollView,而 NSScrollView 又包含一个固定大小的 NSView。 启动程序后,最初显示的滚动条太小,就好像内容大小比
我目前在 NSScrollView 中有一个 NSView。NSView很大,我需要滚动来管理里面的对象。 我想在 NSView 的中心绘制一个静态矩形而不滚动。我想在NSView的drawRect方
我试图弄清楚 NSScrollView 的内容放大是如何工作的: 我有一个 NSScrollView,我试图在其中添加一个包含 NSTextField 的 NSView。 我使用文本字段创建 NSVi
我正在以编程方式在 NSScrollView 内创建 NSTableView,但是当我尝试将框架设置为 ScrollView 时,出现约束错误。 Unable to simultaneously sa
我需要在更大的滚动窗口内使用固定大小的 NSTextView。 IB 要求 TextView 位于它们自己的 NSScrollView 内部,即使它们的最小/最大尺寸是固定的,因此它们实际上不会滚动。
我有一个 NSScrollView,我试图用复选框填充它(NSButton/NSToggleButton)。我添加的复选框数量应该等于数组中的项目数量。这部分正在工作。 以下代码成功地将复选框添加到
我是一名优秀的程序员,十分优秀!