经过一番研究,我能够通过"标识符方式填充NSTableView的某些日期"就像你为每一列分配唯一标识符一样。但是,我想现在如果有一种方法来填充NSTableView而不向列提供标识符以及如何?
更清楚 - 使用自动表列标识符,这里稍作描述:About the automatic table column identifier并且我找到了一种方法如何通过这个表达式枚举或获取列的索引:
//in the objectValueForTableColumn blah blah blah method
int columnIndex = [[aTableColumn identifier] intValue];
return [[myArray objectAtIndex: rowIndex] objectAtIndex: columnIndex];
然而,事实是每列中的columnIndex等于0。 (我在NSTableView中有4个列)
是否可以帮助我如何在不设置标识符的情况下显示数据?非常感谢你!
答案 0 :(得分:4)
首先你不应该得到列的索引,因为可以拖动列,因此可以更改其索引。但是你可以这样做:
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
if ([tableView tableColumns][0] == tableColumn) {
return [self.array[row] firstName];
}
else if ([tableView tableColumns][1] == tableColumn) {
return [self.array[row] lastName];
}
}
另一种方法是检查表头单元格标题。使用此选项,您可以决定要在列中填充的值。类似于:(但在这里你需要手动设置列标题)
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSCell *headerCell = [tableColumn headerCell];
if ([[headerCell title] isEqualToString:@"First Name"]) {
return [self.array[row] firstName];
}
else if ([[headerCell title] isEqualToString:@"Last Name"]) {
return [self.array[row] lastName];
}
return nil;
}
你也可以选择Cocoa-Binding,这里不需要使用标识符!!!
编辑:
因为你没有上课,所以你正在处理基本的C阵列。但是委托返回id
,因此您需要将其类型化为某些Obj-C对象。在以下情况中,我使用NSString
。看屏幕截图:
答案 1 :(得分:0)
But the column identifier is not an Integer
... and therefore your "intValue" will always bring a 0 out of it.
The column Identifier (whether automatic, or provided by the user when defining the table, either programmatically or using the UI editor) is an NSString
. It is used not only for finding an NSColumn, but also for automatically persisting column attributes in NSUserDefaults
.
Another wrong assumption - is that the column identifier has to do with its index. It is NOT. Columns can be re-arranged (by dragging them around, or programmatically) so their index can change.
So - to adapt your code sample to working with column identifiers - I would recommend that you will not work with a 2-dimensional array (with indexes for both rows and columns) but rather use an NSDictionary
for the per-column information, where the key (NSString
) will be the column identifier, and the value - the value of the cell you need.
So: indexes for rows, keys for column identifiers, your sample lines would look like this:
//in the objectValueForTableColumn blah blah blah method
NSString columnIdentifier = [aTableColumn identifier];
return [[myArray objectAtIndex: rowIndex] valueForKey: columnIdentifier];