NSTableView中的自动表列标识符 [英] Automatic table column Indentifier in NSTableView

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

问题描述

经过一番研究,我能够通过标识符方式填充NSTableView一些日期,就像为每个列分配唯一标识符一样。但是,现在我想现在如果有一种方法来填充NSTableView而不提供标识符列和如何?



更清楚的是 - 使用自动表列标识符,稍有描述:关于自动表格列标识符和我找到一种方法如何枚举或获取列的索引通过此表达式:

  //在objectValueForTableColumn blah blah blah方法

int columnIndex = [[aTableColumn identifier] intValue];
return [[myArray objectAtIndex:rowIndex] objectAtIndex:columnIndex];

然而,事实是columnIndex在每列中等于0。 (我有NSTableView的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,这里不需要使用标识符! !!







$ b

由于你没有类,因此你正在处理基本的C数组。但是委托返回 id ,所以你需要将其转换为一些Obj-C对象。在下面的例子中,我使用 NSString 。请参阅屏幕截图:


After bit of research, I was able to populate the NSTableView with some date via the "identifier way" like you assign unique identifier to each column. However, I would like to now if there is a way to populate NSTableView without providing the identifier to the columns and how?

To be more clear - the use of automatic table column identifier, which is slightly described here: About the automatic table column identifier and and I find a way how to enumerate or get the index of the column by this expression:

//in the objectValueForTableColumn blah blah blah method

int columnIndex = [[aTableColumn identifier] intValue];
return [[myArray objectAtIndex: rowIndex] objectAtIndex: columnIndex];

However, the thing is that the columnIndex is equal to 0 in each column. (I have for example 4 columns in NSTableView)

Can you please help me how to display the data without setting the identifier? Thank you very much!

解决方案

First of all you should not get the index of the column, as the columns can be dragged and hence its index can be changed. However you can do as:

- (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];
    }
}

The other way around is to check with the table header cell title. Using this you can decide what value to fill in the column. Something like: (But here you need to set the column header manually)

- (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;
}

Also you can opt for Cocoa-Binding, here no need to use identifiers!!!


Edit:

As you don't have class, infact you are dealing with basic C-array. But the delegate returns id so you need to typecast it to some Obj-C object. In following case I use NSString. See the screen shot :

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

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