NSTableView不会自己存储数据,而是从一个数据源取数据,而这个数据源是要实现NSTableViewDataSource Protocol的方法,而NSTableView的行为则需要实现NSTableViewDelegate Protocol,比如鼠标双击表格,选中表格哪一行等事件,而这些都不需要通过继承NSTableView来重新实现,只需要通过这个协议重写。
NSTableViewDataSource Protocol必须要实现的方法:
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView; 获得表格数据的行数
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row 根据行列加载数据
NSTableViewDelegate Protocol的常用方法:
- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row 根据cell来加载数据,较为常用
- (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row 是否允许编辑当前所在行的数据
- (BOOL)selectionShouldChangeInTableView:(NSTableView *)tableView 能否选中表格的行,返回NO则不能选中表格的每行数据
- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row 指定哪几行能选中或不能选中
- (BOOL)tableView:(NSTableView *)tableView shouldSelectTableColumn:(NSTableColumn *)tableColumn 返回YES则选中某列,该列下所有的元素均呈选中状态
- (void)tableView:(NSTableView *)tableView mouseDownInHeaderOfTableColumn:(NSTableColumn *)tableColumn 鼠标左键按下响应事件函数
- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn 鼠标左键按下响应事件函数
- (void)tableView:(NSTableView *)tableView didDragTableColumn:(NSTableColumn *)tableColumn 移动列位置响应事件函数
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
NSTableView是NSOutlineView的父类,编码比NSOutlineView要简单一些,大体上是差不多的。
1.首先拖动NSTableView然后绑定dataSource和delegate到AppDelegate,正常来说不会绑定到AppDelegate,而是另外创建一个类来接受绑定,这里是为了方便,但项目代码最好是创建多一个类来接受绑定实现。
2.给每一列设定一个标识,显示数据的时候根据标识来显示,我们这里第一列设为name,第二列设为id。
3.自定义一个类来保存每一行的数据。
SimpleData.h
#import <Foundation/Foundation.h>
@interface SimpleData : NSObject
{
NSString *name;
NSString *iD;
}
@property (readwrite,copy) NSString * name;
@property (readwrite,copy) NSString * iD;
@end
SimpleData.m
#import "SimpleData.h"
@implementation SimpleData
@synthesize name;
@synthesize iD;
@end
</span>
4.指定一个类来实现NSTableView的协议方法并加载数据,我们这里直接用AppDelegate。
AppDelegate.h
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
IBOutlet NSTableView *tableView;
NSMutableArray *array;
}
@property (assign) IBOutlet NSWindow *window;
- (void)beginTaskWithCallbackBlock:(void (^)(void))callbackBlock;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "SimpleData.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
array = [NSMutableArray new];
for (int i = 0;i < 10;i ++)
{
SimpleData *data = [SimpleData new];
[data setName:@"呵呵"];
[data setID:[NSString stringWithFormat:@"%d",i]];
[array addObject:data];
}
[tableView reloadData];
}
-(void)dealloc
{
//[array release];
}
//------------------------protocol----------------------------------
//返回表格的行数
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView;
{
return [array count];
}
//用了下面那个函数来显示数据就用不上这个,但是协议必须要实现,所以这里返回nil
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
return nil;
}
//
- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
SimpleData *data = [array objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
if ([identifier isEqualToString:@"name"]) {
NSTextFieldCell *textCell = cell;
[textCell setTitle:[data name]];
}
else if ([identifier isEqualToString:@"id"])
{
NSTextFieldCell *textCell = cell;
[textCell setTitle:[data iD]];
}
}
@end
运行结果:
程序执行数据加载时,各代理方法的调用顺序:
-numberOfRowsInTableView => objectValueForTableColumn => willDisplayCell:(id)cell forTableColumn:
例子代码下载地址 http://download.youkuaiyun.com/detail/yepeng2014/9183297