请解释一下如何在 Cocoa 中控制表视图?

发布于 2024-12-25 06:46:15 字数 1285 浏览 7 评论 0原文

我搜索 Google 来找到一个好的答案,但大多数教程都显示在以前的 Xcode 版本中...... 另外,我不想从 Interface Builder 中拖放单元格,而是以编程方式控制表视图(来自 NSObject 子类文件)。



我目前所做的是这样的:


1. Create a file named tableController.h that is a subclass of NSObject. 2. I create an NSObject Object in my Nib File (and set it as a subclass of tableController).
3. I drag a Table View to my window.
4. I CTRL+Drag from the Table View to my tableController.h so to create the outlet "tableView"
5. I create these functions in the interface file:

-(int)numberOfRowsInTableView:(NSTableView *)cocoaTV;
-(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row;

6. 我实现这样的功能:

-(int)numberOfRowsInTableView:(NSTableView *)cocoaTV{
    return 5;
}
-(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row{
    NSArray *tvArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    NSString *v = [tvArray objectAtIndex:row];
    return v;
}

然后,我按住 CTRL 键并从 Interface Builder 中的对象拖动到表视图以设置数据源并将其设置为委托。

当我构建并运行应用程序时,它显示它已创建 5 行,但在每列的每个单元格中都显示“表视图单元格”。

任何帮助将不胜感激......

I search Google to find a good answer but the most tutorials are shown in previous Xcode Versions...
Also, I don't want to drag-n-drop cells from the Interface Builder, but to control the Table View programmatically (from an NSObject subclass file).

What I currently do is this:

1. Create a file named tableController.h that is a subclass of NSObject.
2. I create an NSObject Object in my Nib File (and set it as a subclass of tableController).
3. I drag a Table View to my window.
4. I CTRL+Drag from the Table View to my tableController.h so to create the outlet "tableView"

5. I create these functions in the interface file:

-(int)numberOfRowsInTableView:(NSTableView *)cocoaTV;
-(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row;

6. I implement the functions like this:

-(int)numberOfRowsInTableView:(NSTableView *)cocoaTV{
    return 5;
}
-(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row{
    NSArray *tvArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    NSString *v = [tvArray objectAtIndex:row];
    return v;
}

Then I CTRL+Drag from the Object in the Interface Builder to the Table View to set the dataSource and to set it as delegate.

When I build and Run the App it shows that it has created the 5 Rows but in every cell in every column it says "Table View Cell".

Any help would be appreciated....

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

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

发布评论

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

评论(1

原来分手还会想你 2025-01-01 06:46:15

-(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row 是错误的..说实话,我不确定它是如何编译的(除非复制/粘贴时出错)。该方法应该如下所示:

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    NSArray *tvArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    NSString *v = [tvArray objectAtIndex:row];
    return v;
}

-(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row is wrong.. i'm not sure how it compiles, to be honest (unless there was an error copy/pasting it). the method should look like:

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    NSArray *tvArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    NSString *v = [tvArray objectAtIndex:row];
    return v;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文