NSTableView是Xcode环境下的一个表格控件,它的效果如下图所示:
第一行的那些标示是可以自己手动修改的,在Xib界面直接双击得到光标便可写入内容。
NSTableView有两种显示属性,View和Cell,具体什么区别我也不是很清楚,我本人用的是Cell(使用View时用我的代码不会显示)。另外使用过程中发现如上图所示的工具栏会有两种模式,如上所示,第一种模式是在我左键单击控件后然后空格两次,控件整体变色,然后在左键单击的情况下偶尔会出现,这个希望高人能给出一个更加简单的调出方式,因为第一种模式在连接委托对象时也会用到。大致说了下使用时的技巧后再来看如何具体使用!
首先,在Xib中放置一个控件,然后设置连接
接着看代码,NSTableView的本质是从一个数据中读出内容然后显示在Cell单元中
//返回数据个数 如果有三行会掉用三次,一次这个一次刷新函数
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
if(aTableView == tableView) return [array[0] count];
else if(aTableView == tableview2) return [array[1] count];
else if(aTableView == tableView3) return [array[2] count];
else if(aTableView == tableview4) return [array[3] count];
else return NULL;
}
//用了下面那个函数来显示数据就用不上这个,但是协议必须要实现,所以这里返回nil
- (id)tableView:(NSTableView *)atableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
return nil;
}
//显示数据 这个东西好像每次都要加载n次
- (void)tableView:(NSTableView *)atableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
LisaData *data;
if(atableView == tableView) data = [array[0] objectAtIndex:row];
else if(atableView == tableview2) data = [array[1] objectAtIndex:row];
else if(atableView == tableView3) data = [array[2] objectAtIndex:row];
else if(atableView == tableview4) data = [array[3] objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
if ([identifier isEqualToString:@"Value"]) {
NSTextFieldCell *textCell = cell;
[textCell setTitle: data.RowHead];
}
else if ([identifier isEqualToString:@"W1"])
{
NSTextFieldCell *textCell = cell;
[textCell setTitle: data.W1];
#ifdef DATACOLOR
if(([data.W1 intValue]>tableData[0][0][0] || [data.W1 intValue]<tableData[0][1][0]) && row==2)
textCell.textColor=[NSColor redColor];
else textCell.textColor=[NSColor blackColor];
#endif
}
else if ([identifier isEqualToString:@"W2"])
{
NSTextFieldCell *textCell = cell;
[textCell setTitle: data.W2];
#ifdef DATACOLOR
if(([data.W2 intValue]>tableData[0][0][1] || [data.W2 intValue]<tableData[0][1][1]) && row==2) textCell.textColor=[NSColor redColor];
else textCell.textColor=[NSColor blackColor];
#endif
}
else if ([identifier isEqualToString:@"W3"])
{
NSTextFieldCell *textCell = cell;
[textCell setTitle: data.W3];
#ifdef DATACOLOR
if(([data.W3 intValue]>tableData[0][0][2] || [data.W3 intValue]<tableData[0][1][2]) && row==2) textCell.textColor=[NSColor redColor];
else textCell.textColor=[NSColor blackColor];
#endif
}
else if ([identifier isEqualToString:@"W4"])
{
NSTextFieldCell *textCell = cell;
[textCell setTitle: data.W4];
#ifdef DATACOLOR
if(([data.W4 intValue]>tableData[0][0][3] || [data.W4 intValue]<tableData[0][1][3]) && row==2) textCell.textColor=[NSColor redColor];
else textCell.textColor=[NSColor blackColor];
#endif
}
else if ([identifier isEqualToString:@"W5"])
{
NSTextFieldCell *textCell = cell;
[textCell setTitle: data.W5];
#ifdef DATACOLOR
if(([data.W5 intValue]>tableData[0][0][4] || [data.W5 intValue]<tableData[0][1][4]) && row==2) textCell.textColor=[NSColor redColor];
else textCell.textColor=[NSColor blackColor];
#endif
}
else if ([identifier isEqualToString:@"W6"])
{
NSTextFieldCell *textCell = cell;
[textCell setTitle: data.W6];
#ifdef DATACOLOR
if(([data.W6 intValue]>tableData[0][0][5] || [data.W6 intValue]<tableData[0][1][5]) && row==2) textCell.textColor=[NSColor redColor];
else textCell.textColor=[NSColor blackColor];
#endif
}
else if ([identifier isEqualToString:@"W7"])
{
NSTextFieldCell *textCell = cell;
[textCell setTitle: data.W7];
#ifdef DATACOLOR
if(([data.W7 intValue]>tableData[0][0][6] || [data.W7 intValue]<tableData[0][1][6]) && row==2) textCell.textColor=[NSColor redColor];
else textCell.textColor=[NSColor blackColor];
#endif
}
else if ([identifier isEqualToString:@"W8"])
{
NSTextFieldCell *textCell = cell;
[textCell setTitle: data.W8];
#ifdef DATACOLOR
if(([data.W8 intValue]>tableData[0][0][7] || [data.W8 intValue]<tableData[0][1][7]) && row==2) textCell.textColor=[NSColor redColor];
else textCell.textColor=[NSColor blackColor];
#endif
}
}
上面两个用到的函数我又跟踪过,调用过程是执行一次第一个函数然后第三个循环次数为行数。至于行数是怎么来的可以直接点击控件进行设置即可。Array数组用来存储值的,它存储了三行的数据,因为每行数据都是以类存储的,所以array存储了三个类成员,之后再根据当前的行数返回0-2任意一个类成员地址,再通过类成员变量对TableView进行刷新。
函数中的条件编译是判断当前表格中的值是否满足要求,否则进行处理。
TableView的数据源我们可以创建一个类来实现
@interface LisaData : NSObject
{
NSString *RowHead;
NSString *Max;
NSString *Min;
NSString *Test;
NSString *W1;
NSString *W2;
NSString *W3;
NSString *W4;
NSString *W5;
NSString *W6;
NSString *W7;
NSString *W8;
}
@property (readwrite,copy) NSString *RowHead;
@property (readwrite,copy) NSString *Max;
@property (readwrite,copy) NSString *Min;
@property (readwrite,copy) NSString *Test;
@property (readwrite,copy) NSString *W1;
@property (readwrite,copy) NSString *W2;
@property (readwrite,copy) NSString *W3;
@property (readwrite,copy) NSString *W4;
@property (readwrite,copy) NSString *W5;
@property (readwrite,copy) NSString *W6;
@property (readwrite,copy) NSString *W7;
@property (readwrite,copy) NSString *W8;
@end
然后在运行中对类进行赋值,再然后把成员变量的值根据Identifier(标示符)赋值到相应的cell就行了
最重要的一点,在对tableview的数据源进行刷新后要掉用[tableView reloadData]表格才会刷新,而且这个方法必须放在主线程中进行掉用才可以刷新。