這是我在這裏的第一個問題。如果以前有人問過,我很抱歉,但在搜索時找不到任何東西。另外,我對編程相當陌生,所以請耐心等待。使用從textFields創建的對象填充NSTableView
基本上我想填充我NSTableView的與通過填寫文本框,然後點擊一個按鈕,創建從自定義類的對象。
這是頁面,用戶將填寫他們正在創建的對象的信息: ]
當點擊完成按鈕,它會帶您到包含表圖,其中新此頁對象,你剛剛創建應補充:
這是我的我的頭文件:
#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'?我嘗試了幾種不同的方式,但無法實現。另外,你還看到了什麼其他問題? – JackLikesPizza
如果有機會,我可以通過使用可可綁定來幫助您使用非常少的代碼完成上述操作。 –