【发布时间】:2014-09-18 13:02:36
【问题描述】:
这是对上一个问题的跟进。 对不起。我不知道如何添加代码或编辑 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
代码:
#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;
来源:
#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
谢谢。我确信我在做一些愚蠢的事情,和/或可能没有做一些必要的事情。我已经尝试了几个小时来解决这个问题。到目前为止没有任何想法。
【问题讨论】:
-
到底是什么问题?表格视图不显示?尝试将表视图的初始化从 MSNavigationPanelView 的 init 方法中移出,并将其移到 MSNavigationPanelView 的
-(void)awakeFromNib方法中 -
那行不通。我的猜测是我错过了一些东西。
-
你找到你之前的问题,进入你名字上方和标签列表下方的空间(离这里几英寸),然后点击“编辑”。
标签: objective-c macos cocoa nstableview