您的位置:首页 > 其它

NSTableView 表格操作:增加,删除,编辑 [2]

2014-07-29 19:15 387 查看
如何在显示的对话框里面,能够显示一个表格?

如果是在Windows系统下的VC MFC会异常简单!

但是在MAC XCODE却不那么容易,还好参照例子,找到了方法:

如何静态的显示一个对话框,请参照我的博客。

调用:

- (IBAction)OnBT_ShowForm2:(id)sender
{
if(!m_form2)
{
m_form2 = [Form_2 new];
}
m_form2.window.title = @"test form2";
[m_form2 showWindow:sender];
}


然后是新建1个文件,选择OS X-》Cocoa-》Objective-C Class

Subclass of: 

NSWindowController

然后 with XLB

这里面需要注意的地方

NSTableView不需要额外的DataSource,而是直接拉到File's owner

然后列里面输入属性直,界面设定就完成。

当然还要把m_table和控件拉在一起。

下面是代码

H

//  Form_2.h

#import <Cocoa/Cocoa.h>

// 本类需要用到TableView里面的操作,所以在这里要增加<NSTableViewDataSource>协议的引用
// 主要使用此协议的四个函数:显示,排序,编辑,等,只有这样NSTableView控件才能增删改显示
// 可以查看NSTableViewDataSource协议里那些函数是必须的。
@interface Form_2 : NSWindowController <NSTableViewDataSource>
{
NSMutableArray *m_table_rows;
IBOutlet NSTableView *m_table;
}

-(void)full_data_to_tableview;
@end


文件内容

M

文件内容

//
//  Form_2.m
//  learn_tableview_windowcontrol
//
//  Created by DMD on 29/7/14.
//  Copyright (c) 2014 EDUCATION. All rights reserved.
//

#import "Form_2.h"

// #import "Person.h"

#import "Form_2_TableView_Column.h"

@interface Form_2 ()

@end

@implementation Form_2

- (id)initWithWindow:(NSWindow *)window
{
self = [super initWithWindow:window];
if (self)
{
// Initialization code here.
}

return self;
}

- (void)windowDidLoad
{
[super windowDidLoad];

// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

/********************************************
Function : init (显示一个窗口,需要初始化)
[手动增加此函数]
*********************************************/
- (id)init
{
//以下的名称是*.xib文件的名称,扩展名不要写在里面(由于在添加窗口的时候,选中with xib就会增加一个同名的窗口文件)
if (![super initWithWindowNibName:@"Form_2"])
{
return nil;
}
m_table_rows = [[NSMutableArray alloc] init];
[self full_data_to_tableview];
return self;
}

// 以下三个函数是为了在其他程序里可以显示本窗口 +++
// 显示窗口
- (IBAction)showWindow:(id)sender
{
[[NSApplication sharedApplication] runModalForWindow:self.window];
}
// 关闭窗口:只有关闭了这个窗口,才会显示其他的窗口,达到静态显示本窗口非目的,否则不能显示为第一个窗口TOP
-(void)closeModalWindow:(id)sender
{
[[NSApplication sharedApplication] stopModal];
}
// 作用同上
- (void)windowWillClose:(NSNotification *)notification
{
[self performSelectorOnMainThread:@selector(closeModalWindow:) withObject:nil waitUntilDone:NO];
}

// up 三个函数是为了在其他程序里可以显示本窗口 ---
// 表格 +++
// 以下是对控件NSTableView控件的支持函数
#pragma mark -
#pragma mark ***** Required Methods (unless bindings are used) *****
//[TableView Must Be]+++++

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
NSInteger index_value=[m_table_rows count];
return index_value;
}

-  (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
Form_2_TableView_Column *rows =[m_table_rows objectAtIndex:row];
return [rows valueForKey:[tableColumn identifier]];
}
//[TableView Must Be]----

#pragma mark -
#pragma mark ***** Optional Methods ***** [++++]
/* NOTE: This method is not called for the View Based TableView.
*/
-(void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
Form_2_TableView_Column *rows = [m_table_rows objectAtIndex:row];
[rows setValue:object forKey:[tableColumn identifier]];
}

- (void)tableView:(NSTableView *)tableView sortDescriptorsDidChange:(NSArray *)oldDescriptors;
{
[m_table_rows sortUsingDescriptors:[tableView sortDescriptors]];
[tableView reloadData];
}
#pragma mark ***** Optional Methods ***** [----]

- (IBAction)OnBT_Refresh:(id)sender
{
[self full_data_to_tableview];
}

- (IBAction)OnBT_Add:(id)sender
{
Form_2_TableView_Column *newEmployee = [[Form_2_TableView_Column alloc] init];
newEmployee.col_item1 = @"Hello1";
newEmployee.col_item2 = @"Hello2";

[m_table_rows addObject:newEmployee];
[m_table reloadData];
}

- (IBAction)OnBT_Delete:(id)sender
{
// Which row is selected?
NSIndexSet *rows = [m_table selectedRowIndexes];

// Is the selection empty?
if ([rows count] == 0)
{
NSBeep();
return;
}

[m_table_rows removeObjectsAtIndexes:rows];
[m_table reloadData];
}
- (IBAction)OnBT_Edit:(id)sender
{
// 获得当前选中表格的行数
NSInteger row_index = [m_table selectedRow];
if(row_index<=-1)
{
return;
}
//首先编辑前,先生成一行数据
Form_2_TableView_Column *row_items = [[Form_2_TableView_Column alloc]init];
row_items.col_item1 = @"123";
row_items.col_item2 = @"456";

//用上面生成的记录,替换当前选中行数据
[m_table_rows replaceObjectAtIndex:row_index withObject:row_items];
//刷新以下表格
[m_table reloadData];
[m_table scrollRowToVisible:[m_table_rows count]-1];
}

-(void)full_data_to_tableview
{
int i=0;
int count_value=0;
NSString *text ;
for (i=0; i<6; i++)
{

Form_2_TableView_Column *newEmployee = [[Form_2_TableView_Column alloc] init];
count_value=count_value+2;
text=[NSString stringWithFormat:@"columne%d",count_value];
newEmployee.col_item1 = text;

count_value=count_value+2;
text=[NSString stringWithFormat:@"columne%d",count_value];
newEmployee.col_item2 = text;

[m_table_rows addObject:newEmployee];
[m_table reloadData];
[m_table scrollRowToVisible:[m_table_rows count] - 1];
}
}

@end


表格的属性文件

H文件

//
//  Form_2_TableView_Column.h
//  learn_tableview_windowcontrol
//
//  Created by DMD on 29/7/14.
//  Copyright (c) 2014 EDUCATION. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Form_2_TableView_Column : NSObject

@property (retain) NSString *col_item1;
@property (retain) NSString *col_item2;

@end


m文件

//
//  Form_2_TableView_Column.m
//  learn_tableview_windowcontrol
//
//  Created by DMD on 29/7/14.
//  Copyright (c) 2014 EDUCATION. All rights reserved.
//

#import "Form_2_TableView_Column.h"

@implementation Form_2_TableView_Column

@synthesize col_item1;
@synthesize col_item2;

//- (id)init
//{
//    self = [super init];
//    if (self) {
//        col_item1 = @"New Person1";
//
//		col_item2 = @"New Person2";
//    }
//    return self;
//}
//- (void)setNilValueForKey:(NSString *)key
//{
////	if ([key isEqual:@"expectedRaise"]) {
////		[self setExpectedRaise:0.0];
////	} else {
//		[super setNilValueForKey:key];
////	}
//}

@end


测试:成功!

今天不上图了,大家反复练习4-5次就会掌握这种表格操作了。
需要注意的地方是:
1.NSTableView控件的数据元是File's owner
2.表格里面的列ID是属性名称,手动输入
3.表格一定要当选中是NSTableView的时候拉到H文件里面
4.需要引用TableVIew协议
5.要使用协议里面4个函数,增删改的话必须都要用。
6.一定别忘记初始化数组
OK!
只要在写程序时,注意以上,就没有问题了。
在学习MAC COCOA开发中祝大家好运!
补充:

- (id)init
{

    //以下的名称是*.xib文件的名称,扩展名不要写在里面(由于在添加窗口的时候,选中with
xib就会增加一个同名的窗口文件)

    if (![super
initWithWindowNibName:@"Form_2"])
    {
       
return nil;
    }
   
m_table_rows = [[NSMutableArray
alloc] init];

   初始化先是表格内容,可以放在这里面    

   [self
full_data_to_tableview];

    return
self;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: