【发布时间】:2015-04-26 02:57:59
【问题描述】:
我有 6 个不同的NSMutable arrays [(Arr(A); Arr(B); etc.],每个代表表中一行的一个单元格的数据。数组本质上是键表示。它们构建在先前的类中,传递给当前类,并且每个类中具有完全相同数量的字符串对象 (63)。表是必不可少的数据表示
问题:
我必须用“keys”和“objects”构建一个
NSDictionary。我如何构建thatDictionary。我尝试了各种方法来逐步遍历计数循环,但均未成功。我可以找到为特定键插入对象的方法。我可以直接从各种数组中加载表格没有
ViewController。我已经布置了表格和 ID,但我无法将ViewController添加到此类 - 我只有“文件所有者”。如果这是最简单的,我该怎么做。-
如果需要,我如何将 6 个数组的元素按顺序作为逗号分隔的字符串放入另一个数组中,该数组成为表格行的输入并因此解析到表格中。
没有代码可以提供,因为我所有的代码尝试都失败了。
这里需要一些具体的指示。
.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;
}
【问题讨论】:
-
也许你可以分享一个单元格的数据是什么样的?
-
并行数组通常很难使用。如何创建一个包含单行中所有单元格属性的自定义类?然后,您的“先前类”将创建一个数组,其中每个元素都是自定义类的一个实例。当你想填充表格时,从数组中获取一个与行号匹配的对象,并要求它提供单元格数据。
-
当你说“一个单元格的数据”时,你是指一列还是一个单元格?
标签: objective-c arrays macos osx-yosemite