从 6 个不同的 NSMutable 数组中的数据填充 ViewBased table
Populating ViewBased table from data in 6 different NSMutable arrays
我有 6 个不同的 NSMutable arrays [(Arr(A); Arr(B); etc.]
,每个代表 table 中一行的一个单元格的数据。数组本质上是键表示。它们是在先前的 classes 中构建的,传递给当前的 class 并且每个对象具有完全相同数量的字符串对象 (63)。 table 是
的基本数据表示
问题:
我必须用“keys”和“objects”构建一个 NSDictionary
。如何构建 thatDictionary
。我尝试了各种方法来逐步执行计数循环但没有成功。我可以找到一种为特定键插入对象的方法。
我可以直接从各种数组 加载 table 而没有 一个 ViewController
吗?我有 table 和 id,但我无法向此 class 添加 ViewController
- 我只有 'Files Owner'。如果这是最简单的,我该怎么做。
如果需要,我如何将 6 个数组的元素按顺序作为另一个数组中的逗号分隔字符串作为 table 行的输入并因此进行解析进入 table.
没有代码可以提供,因为我的所有尝试 w/code 都没有成功。
这里需要一些具体的说明。
.h
// ReportsOutput.h
// Stamp Collection
// Created by Terry Lengel on 4/20/15.
// Copyright (c) 2015 Terry Lengel. All rights reserved.
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import "ReportsClass.h"
@interface ReportsOutput : NSWindowController <NSMenuDelegate,NSTableViewDataSource,NSTableViewDelegate,NSApplicationDelegate>{
// variable and outlet for the table
IBOutlet NSTableView *rptTable;
}
// data element sources
@property(nonatomic,strong) NSMutableArray *tblYrScott;
@property(nonatomic,strong) NSMutableArray *tblYrExt;
@property(nonatomic,strong) NSMutableArray *tblYrYear;
@property(nonatomic,strong) NSMutableArray *tblYrType;
@property(nonatomic,strong) NSMutableArray *tblYrPrice;
@property(nonatomic,strong) NSMutableArray *tblYrDescription;
// the Display data source array for the table
@property(strong) NSMutableArray *rptData;
#pragma mark - Method Declarations
-(BOOL)conditionData;
-(NSDictionary *)makeDictionaryRecord:(NSString*)scott withInfo:(NSString*)ext withInfo:(NSString*)year withInfo:(NSString*)type withInfo:(NSString*)price withInfo:(NSString*)Description;
@end
.m
// ReportsOutput.m
// Stamp Collection
// Created by Terry Lengel on 4/20/15.
// Copyright (c) 2015 Terry Lengel. All rights reserved.
#import "ReportsOutput.h"
#import "ReportsClass.h"
@interface ReportsOutput ()
@end
@implementation ReportsOutput
@synthesize tblYrScott;
@synthesize tblYrExt;
@synthesize tblYrType;
@synthesize tblYrPrice;
@synthesize tblYrYear;
@synthesize tblYrDescription;
@synthesize rptData;
-(id)initWithWindow:(NSWindow *)window{
self = [super initWithWindow:window];
if (self){
// initialize 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.
}
-(void)applicationDidFinishLaunching:(NSNotification *)notification{
//Insert code here to initialize your application
}
// Terminate the app by using the RED button:
-(BOOL)applicationShouldTerminateAfterLastWindowClosed:
(NSApplication *)sender{
return YES;
}
-(void)awakeFromNib{
if (self.conditionData == YES){
[rptTable reloadData];
}
}
-(BOOL)windowShouldClose:(id)sender{
return YES;
}
-(void)performClose:(id)sender{
[self close];
}
#pragma mark - Table View Data Source
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
return rptData.count;
}
-(NSView *) tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSTableCellView *scott = [tableView makeViewWithIdentifier:@"Scott" owner:self];
scott.textField.stringValue = [self.rptData objectAtIndex:row];
return scott;
}
// request for sorting
-(void) tableView:(NSTableView *)tableView sortDescriptorsDidChange:(NSArray *)oldDescriptors{
// table view received sort request
//sort the data, then reload the tableView data:
[rptData sortUsingDescriptors:[rptTable sortDescriptors]];
[rptTable reloadData];
}
-(BOOL)conditionData{
BOOL conditionData = NO;
NSString *rPrice;
NSString *rExt;
NSString *rYear;
NSString *rType;
NSString *rDescription;
for (int b=0; b<[tblYrScott count]; ++b){
//condition source data to remove any null appearences
rExt = [tblYrExt objectAtIndex:b];
if (rExt == (id)[NSNull null] || rExt.length == 0 ){
rExt = @"None";
}else if ([rExt isEqualToString:@" "]){
rExt = @"None";
}else
rExt = [tblYrExt objectAtIndex:b];
rYear = [tblYrYear objectAtIndex:b];
if (rYear == (id)[NSNull null] || rYear.length == 0 ){
rYear = @" ";
}else
rYear = [tblYrYear objectAtIndex:b];
rType = [tblYrType objectAtIndex:b];
if (rType == (id)[NSNull null] || rType.length == 0 ){
rType = @" ";
}else
rType = [tblYrType objectAtIndex:b];
rPrice = [tblYrPrice objectAtIndex:b];
if (rPrice == (id)[NSNull null] || rPrice.length == 0 ){
rPrice = @"n/r";
}else
rPrice = [tblYrPrice objectAtIndex:b];
rDescription = [tblYrDescription objectAtIndex:b];
if (rDescription == (id)[NSNull null] || rDescription.length == 0 ){
rDescription = @" ";
}else
rDescription = [tblYrDescription objectAtIndex:b];
NSDictionary *rptData = @{@"Scott":[tblYrScott objectAtIndex:b],@"Ext":rExt,@"Year":rYear,@"Type":rType,@"Price":rPrice,@"Description":rDescription};
}
//[rptTable reloadData];
return conditionData = YES;
}
-(NSDictionary*) makeDictionaryRecord:(NSString *)scott withInfo: (NSString *)ext withInfo: (NSString *)year withInfo: (NSString *)type withInfo: (NSString *)price withInfo: (NSString *)description{
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:scott,@"Scott",ext,@"Ext",year,@"Year",type,@"Type",price,@"Price",description,@"Description", nil];
return dict;
}
@end
Data Sample:
2015-04-25 12:20:49.251 StampsProjectDev[2981:591183] rptData = {
Description = "Lunar New Year - Horse";
Ext = None;
Price = "n/r";
Scott = 4846;
Type = C;
Year = 2014;
}
2015-04-25 12:20:49.252 StampsProjectDev[2981:591183] rptData = {
Description = "Jimi Hendrix";
Ext = None;
Price = "n/r";
Scott = 4880;
Type = C;
Year = 2014;
}
2015-04-25 12:20:49.252 StampsProjectDev[2981:591183] rptData = {
Description = "Charlton Heston";
Ext = None;
Price = "n/r";
Scott = 4892;
Type = C;
Year = 2014;
}
2015-04-25 12:20:49.252 StampsProjectDev[2981:591183] rptData = {
Description = "Janice Joplin";
Ext = None;
Price = "n/r";
Scott = 4916;
Type = C;
Year = 2014;
}
2015-04-25 12:20:49.252 StampsProjectDev[2981:591183] rptData = {
Description = "Ralph Ellison";
Ext = None;
Price = "n/r";
Scott = 4866;
Type = C;
Year = 2014;
}
Must I build a NSDictionary with 'keys' and 'objects'. How do I build thatDictionary. I have tried various methods stepping through a count loop with no success. I can to find a method for inserting objects for specific keys.
不需要,但建议使用。
// assume six arrays are array0, array1, ...
// assume array0.count == array1.count == etc
for (int i=0; i<array0.count; ++i) {
NSDictionary *di = @{ @"ar0":array0[i], @"ar1":array1[i],... };
}
但更好的想法是创建一个 NSObject 子类,它对您的用户有意义并且具有数组中相应元素的属性(称之为 MeaningfulObject
)...
// in your datasource's interface definition
@property(strong) NSMutableArray *myArrayOfMeaningfulObjects;
self.myArrayOfMeaningfulObjects = [@[] mutableCopy];
// assume array0.count == array1.count == etc
for (int i=0; i<array0.count; ++i) {
MeaningfulObject *mi = [MeaningfulObject meaningfulObjectWithAttribute0:array0[i] attribute1:array1[i]... ];
[self.myArrayOfMeaningfulObjects addObject:mi];
}
Can I load the table directly from the various arrays without a ViewController. I have the table and id's laid out but I have not been able to add a ViewController to this class - I only have 'Files Owner'. If this is easiest how can I do that.
table 自行加载。您的工作是为其提供一个 数据源 ,这是一个表示行的对象数组。大多数人选择让视图包含 table 的视图控制器充当数据源,但 table 的数据源可以是任何对象。 NSTableViewDatasource
协议的两个必需部分是 (1) numberOfRowsInTableView:
像这样:
return self.myArrayOfMeaningfulObjects.count;
和 (2) tableView:viewForTableColumn:row:
像这样:
MeaningfulObject *mrow = self.myArrayOfMeaningfulObjects[indexPath.row];
// configure a tableview cell using mrow's attributes
cell.textLabel.text = mrow.attribute0;
If needed, how do I take elements of the 6 arrays in order as a comma delimited string in another array that becomes the input to a row of of the table and therefore parsed into the table.
不明白这一点,但希望前面隐含了一个好的答案
编辑,在查看代码后,在我看来您几乎已经掌握了它。代码声明了一个数组,它将作为数据源的基础,如下所示:
@property(strong) NSMutableArray *rptData;
很好,但这里犯了一个相应的错误:
NSDictionary *rptData = @{@"Scott":[tblYrScott objectAtIndex:b],@"Ext":rExt,@"Year":rYear,@"Type":rType,@"Price":rPrice,@"Description":rDescription};
这是一个字典,它看起来好像代表 table 中的一行,名称与 NSArray 属性 一样容易混淆。看起来好像字典是建立的,然后在循环的迭代中被放弃,被下一行的对象覆盖。要使其工作,必须将表示行的对象添加到数据源,如下所示:
// renamed rptData dictionary to rowDictionary
NSDictionary *rowDictionary = @{@"Scott":[tblYrScott objectAtIndex:b],@"Ext":rExt,@"Year":rYear,@"Type":rType,@"Price":rPrice,@"Description":rDescription};
[self.rptData addObject:rowDictionary];
我有 6 个不同的 NSMutable arrays [(Arr(A); Arr(B); etc.]
,每个代表 table 中一行的一个单元格的数据。数组本质上是键表示。它们是在先前的 classes 中构建的,传递给当前的 class 并且每个对象具有完全相同数量的字符串对象 (63)。 table 是
问题:
我必须用“keys”和“objects”构建一个
NSDictionary
。如何构建thatDictionary
。我尝试了各种方法来逐步执行计数循环但没有成功。我可以找到一种为特定键插入对象的方法。我可以直接从各种数组 加载 table 而没有 一个
ViewController
吗?我有 table 和 id,但我无法向此 class 添加ViewController
- 我只有 'Files Owner'。如果这是最简单的,我该怎么做。如果需要,我如何将 6 个数组的元素按顺序作为另一个数组中的逗号分隔字符串作为 table 行的输入并因此进行解析进入 table.
没有代码可以提供,因为我的所有尝试 w/code 都没有成功。
这里需要一些具体的说明。
.h
// ReportsOutput.h
// Stamp Collection
// Created by Terry Lengel on 4/20/15.
// Copyright (c) 2015 Terry Lengel. All rights reserved.
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import "ReportsClass.h"
@interface ReportsOutput : NSWindowController <NSMenuDelegate,NSTableViewDataSource,NSTableViewDelegate,NSApplicationDelegate>{
// variable and outlet for the table
IBOutlet NSTableView *rptTable;
}
// data element sources
@property(nonatomic,strong) NSMutableArray *tblYrScott;
@property(nonatomic,strong) NSMutableArray *tblYrExt;
@property(nonatomic,strong) NSMutableArray *tblYrYear;
@property(nonatomic,strong) NSMutableArray *tblYrType;
@property(nonatomic,strong) NSMutableArray *tblYrPrice;
@property(nonatomic,strong) NSMutableArray *tblYrDescription;
// the Display data source array for the table
@property(strong) NSMutableArray *rptData;
#pragma mark - Method Declarations
-(BOOL)conditionData;
-(NSDictionary *)makeDictionaryRecord:(NSString*)scott withInfo:(NSString*)ext withInfo:(NSString*)year withInfo:(NSString*)type withInfo:(NSString*)price withInfo:(NSString*)Description;
@end
.m
// ReportsOutput.m
// Stamp Collection
// Created by Terry Lengel on 4/20/15.
// Copyright (c) 2015 Terry Lengel. All rights reserved.
#import "ReportsOutput.h"
#import "ReportsClass.h"
@interface ReportsOutput ()
@end
@implementation ReportsOutput
@synthesize tblYrScott;
@synthesize tblYrExt;
@synthesize tblYrType;
@synthesize tblYrPrice;
@synthesize tblYrYear;
@synthesize tblYrDescription;
@synthesize rptData;
-(id)initWithWindow:(NSWindow *)window{
self = [super initWithWindow:window];
if (self){
// initialize 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.
}
-(void)applicationDidFinishLaunching:(NSNotification *)notification{
//Insert code here to initialize your application
}
// Terminate the app by using the RED button:
-(BOOL)applicationShouldTerminateAfterLastWindowClosed:
(NSApplication *)sender{
return YES;
}
-(void)awakeFromNib{
if (self.conditionData == YES){
[rptTable reloadData];
}
}
-(BOOL)windowShouldClose:(id)sender{
return YES;
}
-(void)performClose:(id)sender{
[self close];
}
#pragma mark - Table View Data Source
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
return rptData.count;
}
-(NSView *) tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSTableCellView *scott = [tableView makeViewWithIdentifier:@"Scott" owner:self];
scott.textField.stringValue = [self.rptData objectAtIndex:row];
return scott;
}
// request for sorting
-(void) tableView:(NSTableView *)tableView sortDescriptorsDidChange:(NSArray *)oldDescriptors{
// table view received sort request
//sort the data, then reload the tableView data:
[rptData sortUsingDescriptors:[rptTable sortDescriptors]];
[rptTable reloadData];
}
-(BOOL)conditionData{
BOOL conditionData = NO;
NSString *rPrice;
NSString *rExt;
NSString *rYear;
NSString *rType;
NSString *rDescription;
for (int b=0; b<[tblYrScott count]; ++b){
//condition source data to remove any null appearences
rExt = [tblYrExt objectAtIndex:b];
if (rExt == (id)[NSNull null] || rExt.length == 0 ){
rExt = @"None";
}else if ([rExt isEqualToString:@" "]){
rExt = @"None";
}else
rExt = [tblYrExt objectAtIndex:b];
rYear = [tblYrYear objectAtIndex:b];
if (rYear == (id)[NSNull null] || rYear.length == 0 ){
rYear = @" ";
}else
rYear = [tblYrYear objectAtIndex:b];
rType = [tblYrType objectAtIndex:b];
if (rType == (id)[NSNull null] || rType.length == 0 ){
rType = @" ";
}else
rType = [tblYrType objectAtIndex:b];
rPrice = [tblYrPrice objectAtIndex:b];
if (rPrice == (id)[NSNull null] || rPrice.length == 0 ){
rPrice = @"n/r";
}else
rPrice = [tblYrPrice objectAtIndex:b];
rDescription = [tblYrDescription objectAtIndex:b];
if (rDescription == (id)[NSNull null] || rDescription.length == 0 ){
rDescription = @" ";
}else
rDescription = [tblYrDescription objectAtIndex:b];
NSDictionary *rptData = @{@"Scott":[tblYrScott objectAtIndex:b],@"Ext":rExt,@"Year":rYear,@"Type":rType,@"Price":rPrice,@"Description":rDescription};
}
//[rptTable reloadData];
return conditionData = YES;
}
-(NSDictionary*) makeDictionaryRecord:(NSString *)scott withInfo: (NSString *)ext withInfo: (NSString *)year withInfo: (NSString *)type withInfo: (NSString *)price withInfo: (NSString *)description{
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:scott,@"Scott",ext,@"Ext",year,@"Year",type,@"Type",price,@"Price",description,@"Description", nil];
return dict;
}
@end
Data Sample:
2015-04-25 12:20:49.251 StampsProjectDev[2981:591183] rptData = {
Description = "Lunar New Year - Horse";
Ext = None;
Price = "n/r";
Scott = 4846;
Type = C;
Year = 2014;
}
2015-04-25 12:20:49.252 StampsProjectDev[2981:591183] rptData = {
Description = "Jimi Hendrix";
Ext = None;
Price = "n/r";
Scott = 4880;
Type = C;
Year = 2014;
}
2015-04-25 12:20:49.252 StampsProjectDev[2981:591183] rptData = {
Description = "Charlton Heston";
Ext = None;
Price = "n/r";
Scott = 4892;
Type = C;
Year = 2014;
}
2015-04-25 12:20:49.252 StampsProjectDev[2981:591183] rptData = {
Description = "Janice Joplin";
Ext = None;
Price = "n/r";
Scott = 4916;
Type = C;
Year = 2014;
}
2015-04-25 12:20:49.252 StampsProjectDev[2981:591183] rptData = {
Description = "Ralph Ellison";
Ext = None;
Price = "n/r";
Scott = 4866;
Type = C;
Year = 2014;
}
Must I build a NSDictionary with 'keys' and 'objects'. How do I build thatDictionary. I have tried various methods stepping through a count loop with no success. I can to find a method for inserting objects for specific keys.
不需要,但建议使用。
// assume six arrays are array0, array1, ...
// assume array0.count == array1.count == etc
for (int i=0; i<array0.count; ++i) {
NSDictionary *di = @{ @"ar0":array0[i], @"ar1":array1[i],... };
}
但更好的想法是创建一个 NSObject 子类,它对您的用户有意义并且具有数组中相应元素的属性(称之为 MeaningfulObject
)...
// in your datasource's interface definition
@property(strong) NSMutableArray *myArrayOfMeaningfulObjects;
self.myArrayOfMeaningfulObjects = [@[] mutableCopy];
// assume array0.count == array1.count == etc
for (int i=0; i<array0.count; ++i) {
MeaningfulObject *mi = [MeaningfulObject meaningfulObjectWithAttribute0:array0[i] attribute1:array1[i]... ];
[self.myArrayOfMeaningfulObjects addObject:mi];
}
Can I load the table directly from the various arrays without a ViewController. I have the table and id's laid out but I have not been able to add a ViewController to this class - I only have 'Files Owner'. If this is easiest how can I do that.
table 自行加载。您的工作是为其提供一个 数据源 ,这是一个表示行的对象数组。大多数人选择让视图包含 table 的视图控制器充当数据源,但 table 的数据源可以是任何对象。 NSTableViewDatasource
协议的两个必需部分是 (1) numberOfRowsInTableView:
像这样:
return self.myArrayOfMeaningfulObjects.count;
和 (2) tableView:viewForTableColumn:row:
像这样:
MeaningfulObject *mrow = self.myArrayOfMeaningfulObjects[indexPath.row];
// configure a tableview cell using mrow's attributes
cell.textLabel.text = mrow.attribute0;
If needed, how do I take elements of the 6 arrays in order as a comma delimited string in another array that becomes the input to a row of of the table and therefore parsed into the table.
不明白这一点,但希望前面隐含了一个好的答案
编辑,在查看代码后,在我看来您几乎已经掌握了它。代码声明了一个数组,它将作为数据源的基础,如下所示:
@property(strong) NSMutableArray *rptData;
很好,但这里犯了一个相应的错误:
NSDictionary *rptData = @{@"Scott":[tblYrScott objectAtIndex:b],@"Ext":rExt,@"Year":rYear,@"Type":rType,@"Price":rPrice,@"Description":rDescription};
这是一个字典,它看起来好像代表 table 中的一行,名称与 NSArray 属性 一样容易混淆。看起来好像字典是建立的,然后在循环的迭代中被放弃,被下一行的对象覆盖。要使其工作,必须将表示行的对象添加到数据源,如下所示:
// renamed rptData dictionary to rowDictionary
NSDictionary *rowDictionary = @{@"Scott":[tblYrScott objectAtIndex:b],@"Ext":rExt,@"Year":rYear,@"Type":rType,@"Price":rPrice,@"Description":rDescription};
[self.rptData addObject:rowDictionary];