使用textFields创建的对象填充NSTableView

时间:2016-01-11 21:54:04

标签: objective-c cocoa nstableview nstextfield

这是我在这里的第一个问题。如果之前有人问我,我很抱歉,但在搜索时我找不到任何东西。另外,我对编程很陌生,所以请耐心等待。

基本上我尝试使用填写文本字段并单击按钮创建的自定义类中的对象来填充NSTableView。

这是用户填写他们正在创建的对象的信息的页面: Create object page]

单击完成按钮后,它将带您进入包含表视图的页面,其中应添加刚刚创建的新对象: Table View Page

这是我的头文件:

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate, NSTableViewDataSource>{
    NSArray *_tradeArray;
    NSMutableDictionary *_tradeDictionary;}

@property (weak) IBOutlet NSTabView *myTabView;
@property (weak) IBOutlet NSTextField *titleTextField;
@property (weak) IBOutlet NSTextField *volumeTextField;
@property (weak) IBOutlet NSTextField *publisherTextField;
@property (weak) IBOutlet NSTextField *upcTextField;
@property (weak) IBOutlet NSTableView *myTableView;


- (IBAction)tradeLibraryButton:(id)sender;
- (IBAction)backtoMainButton:(id)sender;
- (IBAction)addTradePage:(id)sender;
- (IBAction)cancelAddTrade:(id)sender;
- (IBAction)FinishAddingTrade:(id)sender;
@end

这是我的实施文件:

#import "AppDelegate.h"
#import "JMTradePaperback.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate


-(id)init{
    self = [super init];
    if (self){
        _tradeDictionary = [[NSMutableDictionary alloc]init];
        _tradeArray = [_tradeDictionary allKeys];
        }
    return self;}

- (IBAction)tradeLibraryButton:(id)sender {
    [_myTabView selectTabViewItemAtIndex:1];}

- (IBAction)backtoMainButton:(id)sender {
    [_myTabView selectTabViewItemAtIndex:0];}

- (IBAction)addTradePage:(id)sender {
    [_myTabView selectTabViewItemAtIndex:2];}

- (IBAction)cancelAddTrade:(id)sender {
    [_myTabView selectTabViewItemAtIndex:1];}

- (IBAction)FinishAddingTrade:(id)sender {
    JMTradePaperback *aTrade = [[JMTradePaperback alloc]init];
    [aTrade setTitle:[_titleTextField stringValue]];
    [aTrade setVolume:[_volumeTextField stringValue]];
    [aTrade setPublisher:[_publisherTextField stringValue]];
    [aTrade setUpcCode:[_upcTextField stringValue]];
    [_tradeDictionary setObject:aTrade forKey:[NSString stringWithFormat:@"%@", _upcTextField.stringValue]];
    [_myTabView selectTabViewItemAtIndex:1];
    [_myTableView reloadData];}

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
    return _tradeArray.count;}

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    if ([tableColumn.identifier isEqualToString:@"Title"]){
        JMTradePaperback *item = [_tradeArray objectAtIndex: row];
        return item.title;}
    if ([tableColumn.identifier isEqualToString:@"Volume"]){
        JMTradePaperback *item = [_tradeArray objectAtIndex:row];
        return item.volume;}
    else{
        JMTradePaperback *item = [_tradeArray objectAtIndex:row];
        return item.publisher;}
}
@end

这里有一张tableView连接到AppDelegate作为数据源的图片,图片显示我设置了列标识符: DatasourceColumn Identifier

不确定我在这里做错了什么,但任何帮助都非常感谢。 谢谢。

2 个答案:

答案 0 :(得分:0)

每次修改字典时都需要更新_tradeArray。正如您的代码当前所代表的那样,_tradeArray包含字典的键,就像它初始化时一样,在此之后永远不会更改。

您应该将_tradeArray声明为可变数组(NSMutableArray*)。您可以将其初始化为[[NSMutableArray alloc] init],或者,如果您愿意,也可以[[_tradeDictionary allKeys] mutableCopy]。您可以将每个键(UPC)附加到将其添加到词典的同一点(假设UPC是新的,而不是已经在词典中)。

答案 1 :(得分:0)

如果您已正确添加表列标识符,我认为以下内容应该有效。

// AppDelegate.h
@interface AppDelegate : NSObject <NSApplicationDelegate, NSTableViewDataSource>
@property (weak) IBOutlet NSTabView *myTabView;
@property (weak) IBOutlet NSTextField *titleTextField;
@property (weak) IBOutlet NSTextField *volumeTextField;
@property (weak) IBOutlet NSTextField *publisherTextField;
@property (weak) IBOutlet NSTextField *upcTextField;
@property (weak) IBOutlet NSTableView *myTableView;
...


// AppDelegate.m
@implementation AppDelegate {
    NSArray *tradeArray;
}
@synthesize myTabView,titleTextField,volumeTextField,publisherTextField,upcTextField,myTableView;

- (void)applicationWillFinishLaunching:(NSNotification *)notification {
    tradeArray = [[NSMutableArray alloc] init];
}

- (IBAction)FinishAddingTrade:(id)sender {
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:titleTextField.stringValue,@"title",volumeTextField.stringValue,@"volume",publisherTextField.stringValue,@"publisher",nil];
    [tradeArray addObject:dict];
    [myTableView reloadData];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return tradeArray.count;
}

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    NSString *title = [[tradeArray objectAtIndex:rowIndex] objectForKey:@"title"];
    NSString *vol = [[tradeArray objectAtIndex:rowIndex] objectForKey:@"volume"];
    NSString *pub = [[tradeArray objectAtIndex:rowIndex] objectForKey:@"publisher"];

    if ([tableColumn.identifier isEqualToString:@"Title"]) {
        return  ;
    }
    if ([tableColumn.identifier isEqualToString:@"Volume"]){
        return vol;
    }
    else{
        return pub;
    }
}
相关问题