概要
階段一、實現(xiàn)數(shù)據(jù)的在table view上的顯示、增加。(復習ArrayController)
階段二、通過使用tableview的委托方法tableViewSelectionDidChange來實現(xiàn)將選中項的值賦給文本框。
階段三、通過使用文本框的委托方法controlTextDidChange實現(xiàn)table view內數(shù)據(jù)的動態(tài)修改。
階段一 數(shù)據(jù)的顯示與增加
1、新建工程,打開StoryBoard,添加Push Button、Text Filed、Label、Array Controller、Table View等。

2、創(chuàng)建一個NSObject的子類User,并為其添加firstName與lastName成員。
//User.h
#import <Foundation/Foundation.h>
@interface User : NSObject
@property NSString *firstName;
@property NSString *lastName;
-(instancetype) init;
@end
//User.m
#import "User.h"
@implementation User
-(instancetype) init{
self = [super init];
if(self){
self.firstName = @"Gao";
self.lastName = @"Ben";
}
return self;
}
@end
3、向ViewController.h中添加NSMutableArray類型的成員變量users并在ViewController.m中對其進行初始化。
//ViewController.h
#import <Cocoa/Cocoa.h>
#import "User.h"
@interface ViewController : NSViewController
@property NSMutableArray<User*> *users;
@end
//ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.users = [[NSMutableArray alloc] initWithCapacity:32];
}
4、打開StoryBoard,設置Array Controller的Object Controller中的Class Name為User,將Array Controller的Content Array與ViewController的users進行綁定。


5、將Table view的Table Content與Array Controller進行綁定,并將Add按鈕與Array Controller的Add方法進行綁定。




6、階段一完成,效果如下。

階段二 選中項處理
1、將table view與兩個text filed拖拽大法至ViewController.h中,并添加NSTableViewDelegate協(xié)議,拖拽細節(jié)省略,代碼如下。
#import "ViewController.h"
#import "User.h"
@interface ViewController()<NSTableViewDelegate>
@property (weak) IBOutlet NSTableView *tableView;
@property (weak) IBOutlet NSTextField *text_firstName;
@property (weak) IBOutlet NSTextField *text_lastName;
@property User *currentUser;
@end
2、設置table view的delegate為View Controller,并實現(xiàn)其委托協(xié)議方法。

- (void)tableViewSelectionDidChange:(NSNotification *)notification{
if(self.tableView.selectedRow < 0){
self.text_firstName.stringValue = @"";
self.text_firstName.stringValue = @"";
return ;
}
self.currentUser = self.users[self.tableView.selectedRow];
self.text_firstName.stringValue = self.currentUser.firstName;
self.text_lastName.stringValue = self.currentUser.lastName;
}
3、完成,效果如下。

階段三 數(shù)據(jù)的動態(tài)修改
1、新增“Update”按鈕,拖拽大法至ViewController.m內,命名為btn_update。

2、將表示First Name的文本框與currentUser.firstName綁定起來,將表示Last Name的文本框與currentUser.lastName綁定起來。


3、將First Name文本框、Last Name文本框委托給View Controller。


4、在ViewController.m中添加臨時委托協(xié)議NStextFiledDelegate,并實現(xiàn)controlTextDidChange方法。
#import "ViewController.h"
#import "User.h"
@interface ViewController()<NSTableViewDelegate,NSTextFieldDelegate>
@property (weak) IBOutlet NSTableView *tableView;
@property (weak) IBOutlet NSTextField *text_firstName;
@property (weak) IBOutlet NSTextField *text_lastName;
@property User *currentUser;
@end
@implementation ViewController
//new
- (void)controlTextDidChange:(NSNotification *)obj{
self.currentUser.firstName = _text_firstName.stringValue;
self.currentUser.lastName = _text_lastName.stringValue;
}
- (IBAction)btn_update:(id)sender {
//多余的更新按鈕
NSLog(@"%@ %@",self.currentUser.firstName,self.currentUser.lastName);
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification{
if(self.tableView.selectedRow < 0){
self.text_firstName.stringValue = @"";
self.text_firstName.stringValue = @"";
return ;
}
self.currentUser = self.users[self.tableView.selectedRow];
//self.text_firstName.stringValue = self.currentUser.firstName;
//self.text_lastName.stringValue = self.currentUser.lastName;
}
- (void)viewDidLoad {
[super viewDidLoad];
//4C3TJ-8XH4K-MPQCM-R6C4X-P3C9D
// Do any additional setup after loading the view.
self.users = [[NSMutableArray alloc] initWithCapacity:32];
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
@end
4、完成,效果如下。
