NSTableView未显示 [英] NSTableView is not being displayed

查看:125
本文介绍了NSTableView未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对上一个问题的跟进。
对不起。我不知道如何添加代码或编辑5分钟前写的东西。

This is a follow-up on the previous question. Sorry. I could not figure out how to add code or edit something written over 5 minues ago.

简要摘要。我试图显示自定义/派生TableView通过普通视图。我不是使用IB,而是从代码做的一切。这里的目标是构建应用程序,还要学习Cocoa / OSX编程。这是我的第一次OSX编码尝试。

A brief summary. I am trying to display a customized/derived TableView over a regular View. I am not using IB, but doing everything from the code. The goal here is to build the application, but also to learn Cocoa/OSX programming. This is my first OSX coding attempt.

NSView我想显示我的自定义TableView正在显示很好。请原谅NSLog垃圾。它帮助我了解应用程序生命周期。
标题:

NSView atop of which I would like to display my custom TableView is being displayed fine. Please excuse the NSLog garbage. It helps me to learn about the app lifecycle. Header:

#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派生类。
标题:

Now the NSTableView derived class. Header:

#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

谢谢。我相信我做的事情是愚蠢的,和/或可能不做一些必要的事情。我试图想出这个出了几个小时。到目前为止还没有想法。

Thank you. I am sure that I am doing something stupid, and/or perhaps not doing something necessary. I have tried to figure this out for a couple of hours. No ideas so far.

推荐答案

我想出了需要做什么。

首先,数组初始化必须从+(void)初始化移动到另一个方法。对我来说 - (id)initWithFrame工作正常。

First, array initialization has to be moved from +(void)initialize to another method. For me - (id)initWithFrame works fine.

第二,虽然这不清楚,覆盖NSTableViewDataSource是不够的。

Second, while this was not clear for me, overwriting NSTableViewDataSource is not enough.

必须创建NSTableColumn(s),然后使用NSTableView类的addTableColumn方法将列添加到表中。一旦完成,我们继续setDataSource等。

One has to create NSTableColumn(s) then add the column(s) to the table using addTableColumn method of NSTableView class. Once that is done, we proceed with setDataSource and so on.

这篇关于NSTableView未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆