这是上一个问题的后续。
对不起。我不知道如何添加代码或编辑 5 分钟前编写的内容。
一个简短的总结。我正在尝试在常规视图上显示自定义/派生的 TableView。我没有使用IB,而是从代码中完成一切。这里的目标是构建应用程序,同时也是学习 Cocoa/OSX 编程。这是我第一次尝试 OSX 编码。
我想在其顶部显示我的自定义 TableView 的 NSView 显示良好。请原谅 NSLog 垃圾。它帮助我了解应用程序生命周期。
标题:
#import <Cocoa/Cocoa.h>
#import "MSNavigationTableView.h"
@interface MSNavigationPanelView : NSView
@property (strong, nonatomic) IBOutlet MSNavigationTableView *myNavigationTable;
@end
code:
#import "MSNavigationPanelView.h"
@implementation MSNavigationPanelView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
NSLog(@"Initializing Navigation Panel");
}
self.myNavigationTable = [[MSNavigationTableView alloc] initWithFrame:self.frame];
[self.myNavigationTable setDataSource:self.myNavigationTable];
[self.myNavigationTable setDelegate:self.myNavigationTable];
[self addSubview:self.myNavigationTable];
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
// Drawing code here.
NSLog(@"Drawing navigation view!");
}
@end
现在是 NSTableView 派生类。
标题:
#import <Cocoa/Cocoa.h>
@interface MSNavigationTableView : NSTableView <NSTableViewDataSource>
@end
NSArray *myNavigationArray;
Source:
#import "MSNavigationTableView.h"
@implementation MSNavigationTableView
+ (void)initialize {
NSLog(@"Called NavigationTableView::initialize!");
myNavigationArray = [NSArray arrayWithObjects:@"Call History" @"Contacts", @"Messages", @"Voicemail", nil];
}
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
// Drawing code here.
}
- (NSInteger)numberOfRowsInTableView: (NSTableView *) aTableView
{
return [myNavigationArray count];
}
- (id)tableView: (NSTableView*) aTableView objectValueForTableColumn: (NSTableColumn *)aTableColum row: (NSInteger)rowIndex
{
NSLog([myNavigationArray objectAtIndex:rowIndex]);
return [myNavigationArray objectAtIndex:rowIndex];
}
@end
谢谢。我确信我正在做一些愚蠢的事情,并且/或者也许没有做一些必要的事情。我花了几个小时试图解决这个问题。到目前为止还没有想法。