I have a NSTableView with the delegate and datasource pointing to my controller. I have tried implementing the
我有一个NSTableView,委托和数据源指向我的控制器。我尝试过实现
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
Method, but no matter what I return, the table always shows "Table View Cell" in the data. Any ideas of what I could be doing wrong? Attached are two pics showing that I have the delegates set properly (it also shows the proper number of rows).
方法,但无论我返回什么,该表总是在数据中显示“表视图单元格”。你知道我做错了什么吗?附件是两个图片,显示我已经正确地设置了委托(它还显示了正确的行数)。
Note that I have also just tried returning @"Hello World"
for everything, but I get the same result.
注意,我也刚刚尝试返回@“Hello World”,但是我得到了相同的结果。
6 个解决方案
#1
53
Just change the Content Mode
to Cell Based
for the table view in IB. IB will display Text Cell
as the cell placeholders, which are populated at runtime by whatever you return from tableView:objectValueForTableColumn:row:
只需根据IB中的表视图将内容模式更改为单元格。IB将显示文本单元格作为单元格占位符,该占位符在运行时由从tableView返回的任何内容填充:objectValueForTableColumn:row:
#2
10
Finally figured it out. My cells for some reason seem to contain both a TableCellView AND a text field cell. I removed the Table Cell View
's and now everything is working. I have no idea how I got in that state.
最后想通了。由于某些原因,我的单元格似乎同时包含一个TableCellView和一个文本字段单元格。我删除了表单元格视图,现在一切正常。我不知道我是怎么进入那种状态的。
#3
9
Newer version of XCode:
XCode的新版本:
- Select the table view (make sure the scroll view or any other view is not selected).
- 选择table视图(确保没有选择滚动视图或其他视图)。
- On the right hand side, select the "Attribute Inspector"
- 在右边,选择“属性检查器”
- Change the Content Mode to "Cell Based" instead of "View Based"
- 将内容模式更改为“基于单元格”而不是“基于视图”
- Save your changes and re-run the project.
- 保存更改并重新运行项目。
#4
6
Most of the responses involve converting the table view component to a cell-based table view. If that’s what you want then that’s fine, you’re good to go. At the time the question was asked, cell-based tables were the norm and when Apple changed the window component to a view-based one it obviously caused a lot of confusion. Today, Apple’s docs recommend that you should use view-based rather than cell-based tables.
大多数响应涉及将表视图组件转换为基于单元格的表视图。如果那是你想要的,那就好了,你该走了。当被问到这个问题时,基于cell的表是标准的,当苹果将窗口组件改为基于view的时,显然会引起很多混乱。今天,苹果的文档建议你应该使用基于视图而不是基于单元格的表。
The problem described in the question arises if you use - tableView:objectValueForTableColumn:row:
in your datasource. You cannot use this method with view-based tables, it is only for cell-based tables. Instead you need to use the NSTableViewDelegate
method - tableView:viewForTableColumn:row:
. You don't need to make the object conform to the NSTableViewDelegate
protocol to use this method but it must be set as the delegate of the table.
如果您在数据源中使用—tableView: objectvaluefortablecoln:row: row,则会出现问题。不能对基于视图的表使用此方法,它仅用于基于单元格的表。相反,您需要使用NSTableViewDelegate方法—tableView:viewForTableColumn:row:。使用此方法不需要使对象符合NSTableViewDelegate协议,但必须将其设置为表的委托。
In response to Cœur, quoting from the Apple On-line docs.
为了应对Cœur,引用苹果在线文档。
NSCell-Based Tables Are Still Supported In OS X v10.6 and earlier, each table view cell was required to be a subclass of NSCell. This approach caused limitations when designing complex custom cells, often requiring you to write your own NSCell subclasses. Providing animation, such as progress views, was also extremely difficult. In this document these types of table views are referred to as NSCell-based table views. NSCell-based tables continue to be supported in OS X v10.7 and later, but they’re typically used only to support legacy code. In general, you should use NSView-based tables.
在OS X v10.6中仍然支持基于NSCell的表,在此之前,每个表视图单元格都必须是NSCell的子类。这种方法在设计复杂的自定义单元时造成了限制,通常需要您编写自己的NSCell子类。提供动画(如进度视图)也是极其困难的。在这个文档中,这些类型的表视图被称为基于nscell的表视图。在OS X v10.7和以后的版本中,仍然支持基于nscell的表,但通常只用于支持遗留代码。通常,您应该使用基于nsview的表。
#5
1
It looks like you might be missing the all-important -numberOfRowsInTableView:
data source method. See Table View Programming Guide: (View-based table view) The Required Methods and Table View Programming Guide: (cell-based table view) Providing Data To a Table View Programmatically for details.
看起来您可能漏掉了最重要的-numberOfRowsInTableView:数据源方法。表视图编程指南:(基于视图的表视图)所需的方法和表视图编程指南:(基于单元的表视图)以编程方式向表视图提供数据以获取详细信息。
Basically, the very first NSTableViewDataSource
method that's called is numberOfRowsInTableView:
. Only after you return a non-zero quantity from within that method will the subsequent tableView:objectValueForTableColumn:row:
or tableView:willDisplayCell:forTableColumn:row:
methods be called.
基本上,第一个NSTableViewDataSource方法叫做numberOfRowsInTableView:。只有在从该方法中返回一个非零的量之后,才会调用后续的tableView:objectValueForTableColumn:row:或tableView:willDisplayCell:forTableColumn:row: methods。
#6
0
You're misunderstanding how the result of -tableView:objectValueForTableColumn:row:
is used by the frameworks.
您误解了框架如何使用-tableView: objectvaluefortablecoln:row:的结果。
To do more-or-less what you're trying to accomplish above, override the delegate method -tableView:willDisplayCell:forTableColumn:row:
instead. Here is an example cribbed from one of my own apps:
要实现上面所要实现的目标,可以重写委托方法—tableview:willDisplayCell:forTableColumn:row:改为。这里有一个来自我自己的应用程序的例子:
- (void)tableView:(NSTableView *)tableView
willDisplayCell:(id)cell
forTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row;
{
NSString * displayName = [self.senders objectAtIndex:row];
[cell setTitle:displayName];
[cell setState:[self.selection containsObject:displayName]];
}
This is the "old school" way, using cell-based tables (which are still the default).
这是“旧学校”的方式,使用基于细胞的表格(仍然是默认的)。
#1
53
Just change the Content Mode
to Cell Based
for the table view in IB. IB will display Text Cell
as the cell placeholders, which are populated at runtime by whatever you return from tableView:objectValueForTableColumn:row:
只需根据IB中的表视图将内容模式更改为单元格。IB将显示文本单元格作为单元格占位符,该占位符在运行时由从tableView返回的任何内容填充:objectValueForTableColumn:row:
#2
10
Finally figured it out. My cells for some reason seem to contain both a TableCellView AND a text field cell. I removed the Table Cell View
's and now everything is working. I have no idea how I got in that state.
最后想通了。由于某些原因,我的单元格似乎同时包含一个TableCellView和一个文本字段单元格。我删除了表单元格视图,现在一切正常。我不知道我是怎么进入那种状态的。
#3
9
Newer version of XCode:
XCode的新版本:
- Select the table view (make sure the scroll view or any other view is not selected).
- 选择table视图(确保没有选择滚动视图或其他视图)。
- On the right hand side, select the "Attribute Inspector"
- 在右边,选择“属性检查器”
- Change the Content Mode to "Cell Based" instead of "View Based"
- 将内容模式更改为“基于单元格”而不是“基于视图”
- Save your changes and re-run the project.
- 保存更改并重新运行项目。
#4
6
Most of the responses involve converting the table view component to a cell-based table view. If that’s what you want then that’s fine, you’re good to go. At the time the question was asked, cell-based tables were the norm and when Apple changed the window component to a view-based one it obviously caused a lot of confusion. Today, Apple’s docs recommend that you should use view-based rather than cell-based tables.
大多数响应涉及将表视图组件转换为基于单元格的表视图。如果那是你想要的,那就好了,你该走了。当被问到这个问题时,基于cell的表是标准的,当苹果将窗口组件改为基于view的时,显然会引起很多混乱。今天,苹果的文档建议你应该使用基于视图而不是基于单元格的表。
The problem described in the question arises if you use - tableView:objectValueForTableColumn:row:
in your datasource. You cannot use this method with view-based tables, it is only for cell-based tables. Instead you need to use the NSTableViewDelegate
method - tableView:viewForTableColumn:row:
. You don't need to make the object conform to the NSTableViewDelegate
protocol to use this method but it must be set as the delegate of the table.
如果您在数据源中使用—tableView: objectvaluefortablecoln:row: row,则会出现问题。不能对基于视图的表使用此方法,它仅用于基于单元格的表。相反,您需要使用NSTableViewDelegate方法—tableView:viewForTableColumn:row:。使用此方法不需要使对象符合NSTableViewDelegate协议,但必须将其设置为表的委托。
In response to Cœur, quoting from the Apple On-line docs.
为了应对Cœur,引用苹果在线文档。
NSCell-Based Tables Are Still Supported In OS X v10.6 and earlier, each table view cell was required to be a subclass of NSCell. This approach caused limitations when designing complex custom cells, often requiring you to write your own NSCell subclasses. Providing animation, such as progress views, was also extremely difficult. In this document these types of table views are referred to as NSCell-based table views. NSCell-based tables continue to be supported in OS X v10.7 and later, but they’re typically used only to support legacy code. In general, you should use NSView-based tables.
在OS X v10.6中仍然支持基于NSCell的表,在此之前,每个表视图单元格都必须是NSCell的子类。这种方法在设计复杂的自定义单元时造成了限制,通常需要您编写自己的NSCell子类。提供动画(如进度视图)也是极其困难的。在这个文档中,这些类型的表视图被称为基于nscell的表视图。在OS X v10.7和以后的版本中,仍然支持基于nscell的表,但通常只用于支持遗留代码。通常,您应该使用基于nsview的表。
#5
1
It looks like you might be missing the all-important -numberOfRowsInTableView:
data source method. See Table View Programming Guide: (View-based table view) The Required Methods and Table View Programming Guide: (cell-based table view) Providing Data To a Table View Programmatically for details.
看起来您可能漏掉了最重要的-numberOfRowsInTableView:数据源方法。表视图编程指南:(基于视图的表视图)所需的方法和表视图编程指南:(基于单元的表视图)以编程方式向表视图提供数据以获取详细信息。
Basically, the very first NSTableViewDataSource
method that's called is numberOfRowsInTableView:
. Only after you return a non-zero quantity from within that method will the subsequent tableView:objectValueForTableColumn:row:
or tableView:willDisplayCell:forTableColumn:row:
methods be called.
基本上,第一个NSTableViewDataSource方法叫做numberOfRowsInTableView:。只有在从该方法中返回一个非零的量之后,才会调用后续的tableView:objectValueForTableColumn:row:或tableView:willDisplayCell:forTableColumn:row: methods。
#6
0
You're misunderstanding how the result of -tableView:objectValueForTableColumn:row:
is used by the frameworks.
您误解了框架如何使用-tableView: objectvaluefortablecoln:row:的结果。
To do more-or-less what you're trying to accomplish above, override the delegate method -tableView:willDisplayCell:forTableColumn:row:
instead. Here is an example cribbed from one of my own apps:
要实现上面所要实现的目标,可以重写委托方法—tableview:willDisplayCell:forTableColumn:row:改为。这里有一个来自我自己的应用程序的例子:
- (void)tableView:(NSTableView *)tableView
willDisplayCell:(id)cell
forTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row;
{
NSString * displayName = [self.senders objectAtIndex:row];
[cell setTitle:displayName];
[cell setState:[self.selection containsObject:displayName]];
}
This is the "old school" way, using cell-based tables (which are still the default).
这是“旧学校”的方式,使用基于细胞的表格(仍然是默认的)。