数据源和 NSTableView

发布于 2024-08-29 23:09:16 字数 306 浏览 2 评论 0原文

我知道表源需要一个数据源来保存表视图将显示的数据。 假设我要让我的 AppController 成为我的表视图的数据源,并且我在界面生成器中进行连接。我的问题是,因为我的实际数据将存储在一个数组中,我们称之为 myArray,当我在代码中设置数据源时,我应该这样做吗?

 [tableView setDataSource:myArray]; or this [tableView setDataSource:self];

我对此感到困惑。如果我没有记错的话,使用关键字“self”设置数据源会将其设置为AppController。

I know that table sources need a data source to hold the data that the tableview will display.
Lets' say that I'm going to make my AppController be the data source of my tableview and that I make the connection in interface builder. My question is since my actual data is going to be stored in an array,let's call it myArray, when I set the data source in code should I do this

 [tableView setDataSource:myArray]; or this [tableView setDataSource:self];

I'm confused about this. setting the data source with the keyword "self" would set it to the AppController if I'm not mistaken.

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

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

发布评论

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

评论(1

你在我安 2024-09-05 23:09:16

表视图数据源必须符合 NSTableViewDataSource 协议(在 10.6 之前称为 NSTableDataSource)。

NSArray 不符合此协议,因此不能将其用作数据源。

您需要在 AppController 对象中实现所需的协议方法,并将 AppController 对象指定为表的数据源。

- (void)applicationDidFinishLaunching:(NSNotification*)notification
{
    [tableView setDataSource:self];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
    return [myArray count];
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
    return [myArray objectAtIndex:rowIndex];   
}

A table view data source must conform to the NSTableViewDataSource protocol (called NSTableDataSource prior to 10.6).

NSArray does not conform to this protocol, so you can't use it as a data source.

You need to implement the required protocol methods in your AppController object and assign your AppController object as the table's data source.

- (void)applicationDidFinishLaunching:(NSNotification*)notification
{
    [tableView setDataSource:self];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
    return [myArray count];
}

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