NSTableView绑定问题

发布于 2024-08-30 22:25:17 字数 2411 浏览 1 评论 0原文

我刚刚开始使用 XCode (v3.2.2) 和 Interface Builder,但遇到了问题。

这是我所做的:

我创建了一个类作为 NSTableView 的数据源:

@interface TimeObjectsDS : NSControl {
  IBOutlet NSTableView * idTableView;
  NSMutableArray * timeObjects;
}
@property (assign) NSMutableArray * timeObjects;
@property (assign) NSTableView * idTableView;

- (id) init;
- (void) dealloc;

- (void) addTimeObject: (TimeObj *)timeObject;    

// NSTableViewDataSource Protocol functions
- (int)numberOfRowsInTableView:(NSTableView *)tableView;
- (id)tableView:(NSTableView *)tableView 
            objectValueForTableColumn:(NSTableColumn *)tableColumn row:
            (int)row;


@implementation TimeObjectsDS

@synthesize timeObjects;
@synthesize idTableView;

-(id) init {        
    if (self = [super init]) {
        self.timeObjects = [[NSMutableArray alloc] init];

        TimeObj *timeObject = [[TimeObj alloc] init];
        [timeObject setProjectId:11];
        [timeObject setDescription:@"Heja"];
        [timeObject setRegDate:@"20100331"];
        [timeObject setTimeSum:20.0];

        [timeObjects addObject:timeObject];     

        [timeObject release];

        [idTableView reloadData];        
    }

    return self;
}

- (void) dealloc {
    [idTableView release];
    [timeObjects release];
    [super dealloc];        
}
// Functions
- (void) addTimeObject: (TimeObj *)timeObject {
    [self.timeObjects addObject:timeObject];

    [idTableView reloadData];       
}   

// NSTableViewDataSource Protocol functions
- (int) numberOfRowsInTableView:(NSTableView *)tableView {

    return [self.timeObjects count];
}

- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {

    return [[timeObjects objectAtIndex:row] description];
}
@end

然后我将视图中的 NSTableView 绑定到该数据源,如下所示

: com/tmp/ib_datasource_bindings_big.png" rel="nofollow noreferrer">alt text http://www.og-entertainment.com/tmp/ib_datasource_bindings_big.png

我还将视图 NSTableView 绑定到控制器 idTableView 变量在上面看到的 Interface Builder 中,

在 init 函数中,我将一个元素添加到可变数组中。当我运行应用程序时,这会在 NSTableView 中正确显示。但是,当我向数组添加另一个元素(与 init 中的类型相同)并尝试在视图上调用 [idTableView reloadData] 时,什么也没有发生。 事实上,控制器 idTableView 为空。当使用 NSLog(@"idTableView: %@", idTableView) 打印变量时,我得到“idTableView: (null)”,

我已经没有办法解决这个问题了。我能做些什么来修复绑定有什么想法吗?

I have only just started with XCode (v3.2.2) and Interface Builder and have run into a problem.

Here is what I have done:

I have made a class to be the datasource of a NSTableView:

@interface TimeObjectsDS : NSControl {
  IBOutlet NSTableView * idTableView;
  NSMutableArray * timeObjects;
}
@property (assign) NSMutableArray * timeObjects;
@property (assign) NSTableView * idTableView;

- (id) init;
- (void) dealloc;

- (void) addTimeObject: (TimeObj *)timeObject;    

// NSTableViewDataSource Protocol functions
- (int)numberOfRowsInTableView:(NSTableView *)tableView;
- (id)tableView:(NSTableView *)tableView 
            objectValueForTableColumn:(NSTableColumn *)tableColumn row:
            (int)row;


@implementation TimeObjectsDS

@synthesize timeObjects;
@synthesize idTableView;

-(id) init {        
    if (self = [super init]) {
        self.timeObjects = [[NSMutableArray alloc] init];

        TimeObj *timeObject = [[TimeObj alloc] init];
        [timeObject setProjectId:11];
        [timeObject setDescription:@"Heja"];
        [timeObject setRegDate:@"20100331"];
        [timeObject setTimeSum:20.0];

        [timeObjects addObject:timeObject];     

        [timeObject release];

        [idTableView reloadData];        
    }

    return self;
}

- (void) dealloc {
    [idTableView release];
    [timeObjects release];
    [super dealloc];        
}
// Functions
- (void) addTimeObject: (TimeObj *)timeObject {
    [self.timeObjects addObject:timeObject];

    [idTableView reloadData];       
}   

// NSTableViewDataSource Protocol functions
- (int) numberOfRowsInTableView:(NSTableView *)tableView {

    return [self.timeObjects count];
}

- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {

    return [[timeObjects objectAtIndex:row] description];
}
@end

I have then bound my NSTableView in the View to this datasource like so:

alt text http://www.og-entertainment.com/tmp/ib_datasource_bindings_big.png

I have also bound the View NSTableView to the Controller idTableView variable in Interface Builder seen above

In the init function I add a element to the mutable array. This is displayed correctly in the NSTableView when I run the application. However when I add another element to the array (of same type as in init) and try to call [idTableView reloadData] on the View nothing happens.
In fact the Controller idTableView is null. When printing the variable with NSLog(@"idTableView: %@", idTableView) I get "idTableView: (null)"

Im runing out of ideas how to fix this. Any ideas to what I could do to fix the binding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听不够的曲调 2024-09-06 22:25:17

如果控制器中的 tableview 出口为空,则说明您尚未在 Interface Builder 中连接它。上面的屏幕截图显示了与 TimeObjectsDS 的连接,但这并不意味着很多 - 这是您调用 reloadData 的实例吗?例如,您可能有多个此类的实例。

这只是一种可能性。如果没有更多代码,就不可能列出更多。

顺便说一句,在 MVC 中,将模型对象直接连接到视图被认为是一件坏事。您可能只是错误地使用了术语。

If your tableview outlet in your controller is null, then you haven't connected it in Interface Builder. Your screenshot above shows a connection to TimeObjectsDS, but that doesn't mean a lot - is that the instance that you are calling reloadData from? It is possible that you have more than one instance of this class, for example.

That's just one possibility. Without more code, it's not feasible to list many more.

Incidentally, in MVC it's considered a bad thing to connect a model object directly to a view. You may just be using the terminology incorrectly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文