【问题标题】:Populating ViewBased table from data in 6 different NSMutable arrays从 6 个不同的 NSMutable 数组中的数据填充基于视图的表
【发布时间】:2015-04-26 02:57:59
【问题描述】:

我有 6 个不同的NSMutable arrays [(Arr(A); Arr(B); etc.],每个代表表中一行的一个单元格的数据。数组本质上是键表示。它们构建在先前的类中,传递给当前类,并且每个类中具有完全相同数量的字符串对象 (63)。表是必不可少的数据表示

问题:

  1. 我必须用“keys”和“objects”构建一个NSDictionary。我如何构建thatDictionary。我尝试了各种方法来逐步遍历计数循环,但均未成功。我可以找到为特定键插入对象的方法。

  2. 我可以直接从各种数组中加载表格没有ViewController。我已经布置了表格和 ID,但我无法将 ViewController 添加到此类 - 我只有“文件所有者”。如果这是最简单的,我该怎么做。

  3. 如果需要,我如何将 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


【解决方案1】:

我必须用“keys”和“objects”构建一个 NSDictionary。我如何建立那个字典。我尝试了各种方法来逐步遍历计数循环,但均未成功。我可以找到为特定键插入对象的方法。

不是必需的,但建议使用。

// 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];
}

我可以在没有 ViewController 的情况下直接从各种数组加载表格吗?我已经布置了表格和 ID,但我无法将 ViewController 添加到此类 - 我只有“文件所有者”。如果这是最简单的,我该怎么做。

表格自行加载。你的工作是为它提供一个datasource,它是一个代表行的对象数组。大多数人选择让视图包含表的视图控制器充当数据源,但表的数据源可以是任何对象。 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;

如果需要,我如何将 6 个数组的元素按顺序作为逗号分隔的字符串放在另一个数组中,该数组成为表格行的输入并因此解析到表格中。

不明白这个,但希望前面的内容中隐含了一个好的答案

编辑,在查看代码后,在我看来,您几乎已经掌握了它。代码声明了一个数组,它将作为数据源的基础,如下所示:

@property(strong) NSMutableArray *rptData;

很好,但是这里犯了一个必然的错误:

NSDictionary *rptData = @{@"Scott":[tblYrScott objectAtIndex:b],@"Ext":rExt,@"Year":rYear,@"Type":rType,@"Price":rPrice,@"Description":rDescription};

这是一个字典,它看起来好像代表表中的一行,名称很容易混淆,就像 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];

【讨论】:

  • 问题被标记为OS X,所以不应该是NSTableViewDataSource吗?
  • 是的。已编辑。谢谢。
  • 不要挑剔,但 cellForRowAtIndexPath:numberOfRowsInSection: 不在该协议中。
  • 答案的第一部分帮助我创建了 NSDictionary,但是我仍然没有得到列出的数据。我可以做一个 NSLog 来显示这些数据,所以我知道它就在那里。但是我似乎在将创建的数据数组与表关联起来时遇到了问题。我是否缺少将两者关联起来的例程。
  • @TLL - 请参阅编辑。没有xib,我们可能也可以相处。请确保在 xib 或代码中设置了 table outlet 和 delegate/datasource refs。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-22
  • 1970-01-01
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多