我在表格中滚动时遇到重叠的行问题。数据显示正确,但当我向下滚动时,行中的文本与其他行的文本重叠。
以下是创建NStableView
的代码:
-(void)addTableAt:(NSRect)rect withIdentifier:(NSString *)identifier withIndex:(int) index
{
// Get the table
if ([identifier isEqualToString:@"table_0"]) {
// The view
view = [[NSView alloc] initWithFrame:NSMakeRect(0.0, 0.0, rect.size.width,rect.size.height)];
}
// The ScrollView for the table
NSScrollView *tableScrollView = [[NSScrollView alloc] initWithFrame:NSMakeRect(index*rect.size.width, 0.0, rect.size.width, rect.size.height)];
// The table view
NSTableView *tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(index*rect.size.width, 0.0, rect.size.width, rect.size.height)];
[tableView setIdentifier:identifier];
[tableView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[tableView setHeaderView:nil];
// Set up the right click menu
NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@"Contextual Menu"];
NSString *menuString = [NSString stringWithFormat:@"%@_%d", @"table", index];
NSMenuItem *menuItem = [theMenu insertItemWithTitle:@"Submit to Restore" action:@selector(fileRestore:) keyEquivalent:menuString atIndex:0];
[menuItem setEnabled:YES];
[menuItem setTarget:self];
[tableView setMenu:theMenu];
// Set the column
NSTableColumn *column =[[NSTableColumn alloc]initWithIdentifier:identifier];
[column.headerCell setTitle:@"Header Title"];
[column setWidth:245];
// Add the column tot he table
[tableView addTableColumn:column];
// Set the source and the delegate
[tableView setDataSource:self];
[tableView setDelegate:self];
[tableView setAllowsMultipleSelection: YES];
// Add the table to the scroll view
[tableScrollView setDocumentView:tableView];
// Create the view and the table scroll view
[view setFrame:NSMakeRect(0.0, 0.0, (index+1)*rect.size.width, rect.size.height)];
[view addSubview:tableScrollView];
// Set the view into the main scroll view
[main_scrollview setDocumentView:view];
// Add the main scroll view to the main view
[main_view addSubview:main_scrollview];
[main_view setNeedsDisplay:YES];
}
有关如何解决此问题的任何想法?
答案 0 :(得分:-1)
请勿尝试以编程方式创建NSTableView。
使用Interface Builder并使用滚动视图中包含的内置表视图。 Interface Builder - >工具 - >图书馆 - >对象“NSScrollView中的NSTableView”
有太多事情需要以正确的顺序连接起来。
看起来您需要在应用中添加多个表格? 如果是这种情况,您可以通过多次重复使用nib来创建多个实例。
这里是我用作模板的控制器类的示例代码 实例化一个nib的多个实例。 (窗口/视图管理等细节被遗漏了......这只是一个骨架模板)
在这种情况下,CHelpText是我的控制器,我的笔尖名为CHelpText.xib
@interface CHelpText : NSWindowController {
IBOutlet NSTextView *mTextView;
IBOutlet NSView *mTransparentView;
NSArray *mTopLevelObjects;
}
@property(retain) NSArray *mTopLevelObjects;
+(void)showHelp:(NSString *)iHelpText
title:(NSString *)iHelpTitle;
-(void)awakeFromNib;
- (IBAction) closeWindow: (id)sender
@end
@implementation CHelpText
@synthesize mTopLevelObjects;
+(void)showHelp:(NSString *)iHelpText
title:(NSString *)iHelpTitle
{
////////////// BUILD CHelpText /////////////////////////////////
NSBundle *theClassBundle;
theClassBundle = [NSBundle mainBundle];
NSNib *theNib = [[NSNib alloc] initWithNibNamed:@"CHelpText" bundle:theClassBundle];
CHelpText *theController = [CHelpText alloc];
NSArray *theTopLevelObjects;
[theNib instantiateNibWithOwner:theController
topLevelObjects:&theTopLevelObjects];
theController.mTopLevelObjects = theTopLevelObjects;
[theNib autorelease];
}
-(void)awakeFromNib
{
if ([[self superclass] instancesRespondToSelector:@selector(awakeFromNib)])
[super awakeFromNib];
}
- (IBAction) closeWindow: (id)sender
{
[[self window] performClose:self];
self.mTopLevelObjects = nil;
[self autorelease];
}