在Objective-C中向NSTableView添加/删除行 [英] Add/remove rows to/from NSTableView in Objective-C

查看:91
本文介绍了在Objective-C中向NSTableView添加/删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Cocoa做一个简单的Todo应用程序。我添加了一个类(和一个NSObject到XIB)MATodoController:

I'm making a simple Todo application in Cocoa. I have added a class (and an NSObject to the XIB) MATodoController:

MATodoController.h

MATodoController.h

#import <Cocoa/Cocoa.h>


@interface MATodoController : NSObject
{
    IBOutlet NSTableView *table;
}

- (IBAction)addItem:(id)sender;
- (IBAction)removeItem:(id)sender;

@end

MATodoController.m

MATodoController.m

#import "MATodoController.h"


@implementation MATodoController

- (void)addItem:(id)sender
{

}

- (void)removeItem:(id)sender
{

}

@end

NSTableView和按钮点击调用的两个动作addItem和removeItem。

I have an outlet 'table' to an NSTableView and two actions 'addItem' and 'removeItem' called by button clicks.

是否有办法(当然有办法)可以向NSTableView添加新行/删除所选行(用户可以一次选择多行)?

Is there a way (ofcourse there is a way)How can I add new rows / remove selected rows to and from an NSTableView (users can select multiple rows at once)?

提前感谢。

还有一件事:NSTableView只有一列(由复选框组成)。

Oh, one more thing: The NSTableView has only one column (which consists of checkboxes).

推荐答案

p>在Cocoa中,你不能直接向NSTableView添加/删除行。在你的控制器中,你可能想采用NSTableDataSource协议,它有两个重要的方法,你需要实现,以使其工作:

In Cocoa, you don't really add/remove rows to a NSTableView directly. In your controller, you might want to adopt the NSTableDataSource protocol, which has 2 important methods you need to implement in order to get this working:

- (int) numberOfRowsInTableView:(NSTableView *)aTableView
- (id) tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex

这些方法将响应表视图发送到其数据源(在Interface Builder中配置)的消息,以便在表中填充数据。在这些方法实现中,您必须通过查询您制定的任何数据存储,返回方法所需的信息(行数;特定行中的单元格的值)。

These methods will respond to messages the table view sends to it's data source (configured in Interface Builder) in order to populate the table with rows of data. In these method implementations, you will have to return the information that the method requires (the number of rows; the value of a cell in a particular row) by querying whatever data store you've worked out.

您的 addItem removeItem 方法还需要存储/删除行数据任何代表你已经制作)。

Your addItem and removeItem methods will also need to store/delete row data (in whatever representation you've concocted). You might create a new class that represents each row and store them in an NSMutableDictionary, for instance.

除了Apple的文档,这里有一个此任务的良好教程

Besides Apple's docs, here's a good tutorial for this task.

祝你好运!

这篇关于在Objective-C中向NSTableView添加/删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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