具有多列的NSTableView [英] NSTableView with multiple columns

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

问题描述

什么是一个简单的方法来设置我的NSTableView多个列,只显示一列中的某些数据。我已经设置了IBOutlets,但我不知道从哪里开始。

What is an easy way to set up my NSTableView with multiple columns to only display certain data in one column. I have the IBOutlets set up, but I don't know where to go from there.

推荐答案

假设你不使用Cocoa Bindings / Core Data中,您可以通过从 NSTableViewDataSource protocol。通常你的控制器将实现协议,所以打开控制器.m文件并将这些方法添加到控制器的 @implementation

Assuming you're not using Cocoa Bindings/Core Data, you can display data in an NSTableView by implementing two methods from the NSTableViewDataSource protocol. Typically your controller will implement the protocol, so open the controller .m file and add these methods to the controller's @implementation:

- (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView {
  return 25;  // fill this out
}

– (id) tableView:(NSTableView*)tableView
       objectValueForTableColumn:(NSTableColumn*)column
       row:(int)row {
  return row % 3 ? @"Tick..." : @"BOOM!";  // fill this out
}

您需要设置表的 dataSource 属性到控制器。在Interface Builder控件中 - 从表视图拖动到控制器,并设置 dataSource 。现在构建并运行,您应该在表中看到您的数据。

You need to set the table's dataSource property to the controller. In Interface Builder control-drag from the table view to the controller and set dataSource. Now build and run and you should see your data in the table.

如果只想填写一列,请将IBOutlet NSTableColumn *添加到控制器;让我们把它叫做 explosiveColumn 。在Interface Builder中,控制 - 从控制器拖动到要填写的列,并设置 explosColumn 。然后,在tableView:objectValueForTableColumn:row:中,您可以测试参数是否与插座设置为相同的对象:

If you only want to fill out one column, add an IBOutlet NSTableColumn* to your controller; let's call it explosiveColumn. In Interface Builder, control-drag from the controller to the column you want to fill in and set explosiveColumn. Then, in tableView:objectValueForTableColumn:row: you can test if the column parameter is the same object as the one that the outlet is set to:

– (id) tableView:(NSTableView*)tableView
       objectValueForTableColumn:(NSTableColumn*)column
       row:(int)row {
  if (column == explosiveColumn) {
    return row % 3 ? @"Tick..." : @"BOOM!";
  } else {
    // other columns blank for now
    return nil;
  }
}

本教程可能有用: http://www.cocoadev.com/index.pl?NSTableViewTutorial

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

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