Display, add and delete data from an NSTableView Implement NSTableView.
2013-10-26 23:45
375 查看
http://juliuspaintings.co.uk/cgi-bin/paint_css/animatedPaint/025-NSTableView.pl
Problem: To create a 3 collumn NSTableView, with ability to edit entries, insert rows and delete rows.
Call it: 025-NSTableView
Now create the classes: MyDataObject of type NSObject, MyTableController of type NSControl.
(Note it need not be of type NSControl!)
Code these as shown below and save.
Note that MyTableController is the Data Source for the NSTableView which expects to find there the methods:
- (int)numberOfRowsInTableView:
- (id) tableView:objectValueForTableColumn:row:
- (void)tableView:setObjectValue:forTableColumn:row:
Note also that although I have made MyTableController not only the Data Source for the NSTableView but also its Delegate, I have not used any of its delegate methods.
The following links I found useful:
CocoaDev: NSTableView
CocoaDev: NSTableViewTutorial
CocoaDev: AnotherNSTableViewTutorial
O'Reilley MacDevCenter: Understanding the NSTableView Class
Rixstep: NSTableView
Quickies for NSTableView
And ofcourse NSTableView Class Reference
// MyTableController.h
// 025-NSTableView
//
#import <Cocoa/Cocoa.h>
#import "MyDataObject.h"
//@interface MyTableController : NSControl {
@interface MyTableController :
NSObject {
NSMutableArray * nsMutaryDataObj;
IBOutlet
NSTableView * idTableView;
}
@property (assign)
NSMutableArray * nsMutaryDataObj;
@property (assign)
NSTableView * idTableView;
- (IBAction)addAtSelectedRow:(id)pId;
- (IBAction)deleteSelectedRow:(id)pId;
- (void)addRow:(MyDataObject *)pDataObj;
- (int)numberOfRowsInTableView:(NSTableView *)pTableViewObj;
- (id) tableView:(NSTableView *)pTableViewObj
objectValueForTableColumn:(NSTableColumn *)pTableColumn
row:(int)pRowIndex;
- (void)tableView:(NSTableView *)pTableViewObj
setObjectValue:(id)pObject
forTableColumn:(NSTableColumn *)pTableColumn
row:(int)pRowIndex;
@end
// MyTableController.m
// 025-NSTableView
//
#import "MyTableController.h"
@implementation MyTableController
@synthesize nsMutaryDataObj;
@synthesize idTableView;
- (void)awakeFromNib {
self.nsMutaryDataObj = [[NSMutableArray
alloc]init];
int i;
for (i =
0; i < 10; i ++) {
NSString * zStr1 = [[NSString
alloc]initWithFormat:@"%d",(i+1)*10];
NSString * zStr2 = [[NSString
alloc]initWithFormat:@"%d",(i+1)*100];
NSString * zStr3 = [[NSString
alloc]initWithFormat:@"%d",(i+1)*1000];
MyDataObject * zDataObject = [[MyDataObject
alloc]initWithString1:zStr1
andString2:zStr2
andString3:zStr3];
[self.nsMutaryDataObj
addObject:zDataObject];
} // end for
[idTableView
reloadData];
} // end awakeFromNib
- (IBAction)addAtSelectedRow:(id)pId {
if ([idTableView
selectedRow] > -1) {
NSString * zStr1 =
@"Text Cell 1";
NSString * zStr2 =
@"Text Cell 2";
NSString * zStr3 =
@"Text Cell 3";
MyDataObject * zDataObject = [[MyDataObject
alloc]initWithString1:zStr1
andString2:zStr2
andString3:zStr3];
[self.nsMutaryDataObj
insertObject:zDataObject
atIndex:[idTableView
selectedRow]];
[idTableView
reloadData];
} // end if
} // end deleteSelectedRow
- (IBAction)deleteSelectedRow:(id)pId {
if ([idTableView
selectedRow] > -1) {
[self.nsMutaryDataObj
removeObjectAtIndex:[idTableView
selectedRow]];
[idTableView
reloadData];
} // end if
} // end deleteSelectedRow
- (void)addRow:(MyDataObject *)pDataObj {
[self.nsMutaryDataObj
addObject:pDataObj];
[idTableView
reloadData];
} // end addRow
- (int)numberOfRowsInTableView:(NSTableView *)pTableViewObj {
return [self.nsMutaryDataObj
count];
} // end numberOfRowsInTableView
- (id) tableView:(NSTableView *)pTableViewObj
objectValueForTableColumn:(NSTableColumn *)pTableColumn
row:(int)pRowIndex {
MyDataObject * zDataObject = (MyDataObject *)
[self.nsMutaryDataObj
objectAtIndex:pRowIndex];
if (! zDataObject) {
NSLog(@"tableView: objectAtIndex:%d = NULL",pRowIndex);
return
NULL;
} // end if
NSLog(@"pTableColumn identifier = %@",[pTableColumn
identifier]);
if ([[pTableColumn
identifier] isEqualToString:@"Col_ID1"]) {
return [zDataObject
nsStrName1];
}
if ([[pTableColumn
identifier] isEqualToString:@"Col_ID2"]) {
return [zDataObject
nsStrName2];
}
if ([[pTableColumn
identifier] isEqualToString:@"Col_ID3"]) {
return [zDataObject
nsStrName3];
}
NSLog(@"***ERROR** dropped through pTableColumn identifiers");
return
NULL;
} // end tableView:objectValueForTableColumn:row:
- (void)tableView:(NSTableView *)pTableViewObj
setObjectValue:(id)pObject
forTableColumn:(NSTableColumn *)pTableColumn
row:(int)pRowIndex {
MyDataObject * zDataObject = (MyDataObject *)
[self.nsMutaryDataObj
objectAtIndex:pRowIndex];
if ([[pTableColumn
identifier] isEqualToString:@"Col_ID1"]) {
[zDataObject
setNsStrName1:(NSString *)pObject];
}
if ([[pTableColumn
identifier] isEqualToString:@"Col_ID2"]) {
[zDataObject
setNsStrName2:(NSString *)pObject];
}
if ([[pTableColumn
identifier] isEqualToString:@"Col_ID3"]) {
[zDataObject
setNsStrName3:(NSString *)pObject];
}
} // end tableView:setObjectValue:forTableColumn:row:
Bring up Interface Builder by double clicking on MainMenu.nib.
Drag a NSTableView and two buttons onto the window and name them as shown.
Note: to insert the third collumn go to the Attributes panel of the Inspector and set the number of collumns to three.
Note: to make the collumns of equal size select the NSScrollView, go into the Size panel of the Inspector and set the width of the view to 3n + 26, where n is the required width of each collumn and 26 is for the rhs scroll bar.
Next, select each collumn in turn and set its width to n. In my example n = 95.
![](http://juliuspaintings.co.uk/pictureData/animatedPaint/025-1-Design-In-Inteface-Builder.jpg)
![](http://juliuspaintings.co.uk/pictureData/animatedPaint/025-2-Set-the-number-of-NSTableView-collumns-to-3.jpg)
![](http://juliuspaintings.co.uk/pictureData/animatedPaint/025-25-set-identifier-to-collumn.jpg)
![](http://juliuspaintings.co.uk/pictureData/animatedPaint/025-3-set-NSObject-to-MyTableController.jpg)
![](http://juliuspaintings.co.uk/pictureData/animatedPaint/025-4-set-DataSource-and-Delegate-to-MyTableController.jpg)
![](http://juliuspaintings.co.uk/pictureData/animatedPaint/025-5-Link-MyTableController-to-NSTableView.jpg)
Save everything and Run.
![](http://juliuspaintings.co.uk/pictureData/animatedPaint/025-6-the-NSTableView-Application.jpg)
Download 025-NSTableView.zip (2.3 MB)
Problem: To create a 3 collumn NSTableView, with ability to edit entries, insert rows and delete rows.
Implementation.
Create a new project in XCode: File->New Project->Cocoa ApplicationCall it: 025-NSTableView
Now create the classes: MyDataObject of type NSObject, MyTableController of type NSControl.
(Note it need not be of type NSControl!)
Code these as shown below and save.
Note that MyTableController is the Data Source for the NSTableView which expects to find there the methods:
- (int)numberOfRowsInTableView:
- (id) tableView:objectValueForTableColumn:row:
- (void)tableView:setObjectValue:forTableColumn:row:
Note also that although I have made MyTableController not only the Data Source for the NSTableView but also its Delegate, I have not used any of its delegate methods.
The following links I found useful:
CocoaDev: NSTableView
CocoaDev: NSTableViewTutorial
CocoaDev: AnotherNSTableViewTutorial
O'Reilley MacDevCenter: Understanding the NSTableView Class
Rixstep: NSTableView
Quickies for NSTableView
And ofcourse NSTableView Class Reference
// MyTableController.h
// 025-NSTableView
//
#import <Cocoa/Cocoa.h>
#import "MyDataObject.h"
//@interface MyTableController : NSControl {
@interface MyTableController :
NSObject {
NSMutableArray * nsMutaryDataObj;
IBOutlet
NSTableView * idTableView;
}
@property (assign)
NSMutableArray * nsMutaryDataObj;
@property (assign)
NSTableView * idTableView;
- (IBAction)addAtSelectedRow:(id)pId;
- (IBAction)deleteSelectedRow:(id)pId;
- (void)addRow:(MyDataObject *)pDataObj;
- (int)numberOfRowsInTableView:(NSTableView *)pTableViewObj;
- (id) tableView:(NSTableView *)pTableViewObj
objectValueForTableColumn:(NSTableColumn *)pTableColumn
row:(int)pRowIndex;
- (void)tableView:(NSTableView *)pTableViewObj
setObjectValue:(id)pObject
forTableColumn:(NSTableColumn *)pTableColumn
row:(int)pRowIndex;
@end
// MyTableController.m
// 025-NSTableView
//
#import "MyTableController.h"
@implementation MyTableController
@synthesize nsMutaryDataObj;
@synthesize idTableView;
- (void)awakeFromNib {
self.nsMutaryDataObj = [[NSMutableArray
alloc]init];
int i;
for (i =
0; i < 10; i ++) {
NSString * zStr1 = [[NSString
alloc]initWithFormat:@"%d",(i+1)*10];
NSString * zStr2 = [[NSString
alloc]initWithFormat:@"%d",(i+1)*100];
NSString * zStr3 = [[NSString
alloc]initWithFormat:@"%d",(i+1)*1000];
MyDataObject * zDataObject = [[MyDataObject
alloc]initWithString1:zStr1
andString2:zStr2
andString3:zStr3];
[self.nsMutaryDataObj
addObject:zDataObject];
} // end for
[idTableView
reloadData];
} // end awakeFromNib
- (IBAction)addAtSelectedRow:(id)pId {
if ([idTableView
selectedRow] > -1) {
NSString * zStr1 =
@"Text Cell 1";
NSString * zStr2 =
@"Text Cell 2";
NSString * zStr3 =
@"Text Cell 3";
MyDataObject * zDataObject = [[MyDataObject
alloc]initWithString1:zStr1
andString2:zStr2
andString3:zStr3];
[self.nsMutaryDataObj
insertObject:zDataObject
atIndex:[idTableView
selectedRow]];
[idTableView
reloadData];
} // end if
} // end deleteSelectedRow
- (IBAction)deleteSelectedRow:(id)pId {
if ([idTableView
selectedRow] > -1) {
[self.nsMutaryDataObj
removeObjectAtIndex:[idTableView
selectedRow]];
[idTableView
reloadData];
} // end if
} // end deleteSelectedRow
- (void)addRow:(MyDataObject *)pDataObj {
[self.nsMutaryDataObj
addObject:pDataObj];
[idTableView
reloadData];
} // end addRow
- (int)numberOfRowsInTableView:(NSTableView *)pTableViewObj {
return [self.nsMutaryDataObj
count];
} // end numberOfRowsInTableView
- (id) tableView:(NSTableView *)pTableViewObj
objectValueForTableColumn:(NSTableColumn *)pTableColumn
row:(int)pRowIndex {
MyDataObject * zDataObject = (MyDataObject *)
[self.nsMutaryDataObj
objectAtIndex:pRowIndex];
if (! zDataObject) {
NSLog(@"tableView: objectAtIndex:%d = NULL",pRowIndex);
return
NULL;
} // end if
NSLog(@"pTableColumn identifier = %@",[pTableColumn
identifier]);
if ([[pTableColumn
identifier] isEqualToString:@"Col_ID1"]) {
return [zDataObject
nsStrName1];
}
if ([[pTableColumn
identifier] isEqualToString:@"Col_ID2"]) {
return [zDataObject
nsStrName2];
}
if ([[pTableColumn
identifier] isEqualToString:@"Col_ID3"]) {
return [zDataObject
nsStrName3];
}
NSLog(@"***ERROR** dropped through pTableColumn identifiers");
return
NULL;
} // end tableView:objectValueForTableColumn:row:
- (void)tableView:(NSTableView *)pTableViewObj
setObjectValue:(id)pObject
forTableColumn:(NSTableColumn *)pTableColumn
row:(int)pRowIndex {
MyDataObject * zDataObject = (MyDataObject *)
[self.nsMutaryDataObj
objectAtIndex:pRowIndex];
if ([[pTableColumn
identifier] isEqualToString:@"Col_ID1"]) {
[zDataObject
setNsStrName1:(NSString *)pObject];
}
if ([[pTableColumn
identifier] isEqualToString:@"Col_ID2"]) {
[zDataObject
setNsStrName2:(NSString *)pObject];
}
if ([[pTableColumn
identifier] isEqualToString:@"Col_ID3"]) {
[zDataObject
setNsStrName3:(NSString *)pObject];
}
} // end tableView:setObjectValue:forTableColumn:row:
Bring up Interface Builder by double clicking on MainMenu.nib.
Drag a NSTableView and two buttons onto the window and name them as shown.
Note: to insert the third collumn go to the Attributes panel of the Inspector and set the number of collumns to three.
Note: to make the collumns of equal size select the NSScrollView, go into the Size panel of the Inspector and set the width of the view to 3n + 26, where n is the required width of each collumn and 26 is for the rhs scroll bar.
Next, select each collumn in turn and set its width to n. In my example n = 95.
![](http://juliuspaintings.co.uk/pictureData/animatedPaint/025-1-Design-In-Inteface-Builder.jpg)
Interface Builder: Do the layout
![](http://juliuspaintings.co.uk/pictureData/animatedPaint/025-2-Set-the-number-of-NSTableView-collumns-to-3.jpg)
Set the number of NSTableView collumns to 3
Select each collumn in turn and in the Attributes panel of the Inspector give it an identifier by which to identify it in the code. Here the identifier is Col_ID1.![](http://juliuspaintings.co.uk/pictureData/animatedPaint/025-25-set-identifier-to-collumn.jpg)
Interface Builder: Set the collumn identifier to Col_ID1
Drag an NSObject onto the MainMenu.xib and set the object to MyTableController.![](http://juliuspaintings.co.uk/pictureData/animatedPaint/025-3-set-NSObject-to-MyTableController.jpg)
Interface Builder: set NSObject to MyTableController
Select the NSTableView and in the Connections panel of the Inspector set the DataSource and Delegate outlets to MyTableController.![](http://juliuspaintings.co.uk/pictureData/animatedPaint/025-4-set-DataSource-and-Delegate-to-MyTableController.jpg)
Interface Builder: set DataSource and delegate to be MyTableController
Link the idTableViewOutlet of MyTableController to the NSTableView.![](http://juliuspaintings.co.uk/pictureData/animatedPaint/025-5-Link-MyTableController-to-NSTableView.jpg)
Interface Builder: Link MyTableController to the NSTableView
Link the two buttons to their respective IBActions in MyTableController.Save everything and Run.
![](http://juliuspaintings.co.uk/pictureData/animatedPaint/025-6-the-NSTableView-Application.jpg)
the working NSTableView Application
If you want to download the code
Click the Download Link to obtain 025-NSTableView.zip file of this whole OS X 10.5 Leopard program.Download 025-NSTableView.zip (2.3 MB)
相关文章推荐
- How to Implement Add/Edit/Delete/View with PHP using Angular JS (Part-2)
- How to Implement Add/Edit/Delete/View with PHP using Angular JS (Part-1)
- An Algorithm for Surface Encoding and Reconstruction From 3D Point Cloud Data
- 开放源码的对象关系映射工具ORM.NET 查看和显示数据 View and Display data using ORM.NET
- Delete a Row from UITableView and Model-View-Controller
- Display data from multiple sources in a single Data View(转office.Microsoft)
- PHP Tutorials : Add, View, Edit & Delete from to MySQL database, Live Example (News System)
- LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design
- PHP Tutorials : Add, View, Edit & Delete from to MySQL database, Live Example (News System)
- Tutorial: Analyzing sales data from Excel and an OData feed
- [Nuxt] Use Vuex Actions to Delete Data from APIs in Nuxt and Vue.js
- [Nuxt] Add Arrays of Data to the Vuex Store and Display Them in Vue.js Templates
- 解决InvalidDataAccessApiUsageException: Executing an update/delete query
- 11. 23. 4. 读写一个多边形 Reading Basic Data From an Object Stream
- using iscroll.js and iscroll jquery plugin in android webview to scroll div and ajax load data.
- 关于Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.的问题
- How To Display Hierarchical Data by Using Nested Repeater Controls and Visual
- UPDATE {0} SET DisplayOrder=(SELECT COUNT(1) FROM {0} b WHERE b.DisplayOrder<a.DisplayOrder AND {1})
- View and Data API Tips: Constrain Viewer Within a div Container
- LeetCode211:Add and Search Word - Data structure design