gpt4 book ai didi

objective-c - 使用从 textFields 创建的对象填充 NSTableView

转载 作者:行者123 更新时间:2023-12-03 17:19:51 24 4
gpt4 key购买 nike

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

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

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

单击“完成”按钮后,您将进入包含 TableView 的页面,应在其中添加刚刚创建的新对象: 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 作为数据源的图片,以及显示我设置了列标识符的图片: Datasource , Column Identifier

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

最佳答案

每次修改字典时都需要更新_tradeArray。正如您当前的代码所示,_tradeArray 包含字典初始化时的键,并且此后永远不会更改。

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

关于objective-c - 使用从 textFields 创建的对象填充 NSTableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34731850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com