[Cocoa]_[NSTableView]_[基本使用]

本文介绍了NSTableView的基本使用,包括其数据源(NSTableViewDataSource)和代理(NSTableViewDelegate)协议。数据源负责提供表格数据,代理处理用户交互事件,如单元格显示、编辑权限和行选中。文章还列举了必须实现的协议方法,并提到NSTableView是NSOutlineView的父类,编码相对简单。最后,提供了简单的数据模型和代理实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值