0

我知道这是一个典型的问题,我发现很多线程都在谈论这个,但我无法真正理解我的实现问题出在哪里。我设置了所有内容,并且能够看到加载到我的 NSTableView 内的 NSMutableArray 中的数据。问题是改变这些数据。我要做的是:

-(void) setData:(NSMutableArray*)data{
  for (int i=0; i<[data count]; i++){
      [list addObject:[data objectAtIndex:i]];
  }
  [self reloadData];
}

我设置了一些调试日志以查看该方法是否

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row

在 [self reloadData] 之后被调用(如我所料),但事实并非如此。

需要明确的是,“setData”被放置在 TableViewController 中,它扩展了 NSTableView 并实现了 NSTableViewDataSource。所以“self”应该是正确的,因为指的是界面上显示的实际 NSTableView。

编辑

这是 TableViewController.m 的代码:

#import "TableViewController.h"
#import "Transaction.h"

@implementation TableViewController

- (id) init
{
    self = [super init];
    if (self){
        list = [self loadData];
        //list = [[NSMutableArray alloc]init];
    }
    NSLog(@"init");

    return self;
}

-(void) setData:(NSMutableArray*)data{
    NSLog(@"setData");
    [list removeAllObjects];
    for (int i=0; i<[data count]; i++){
        [list addObject:[data objectAtIndex:i]];
    }
    [self reloadData];
}

-(void)reloadData{
    NSLog(@"reloadData. List: %ld",[list count]);
    [super reloadData];
}

- (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView{
    return [list count];
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    NSLog(@"tableView");
    Transaction *t = [list objectAtIndex:row];

    if ([[tableColumn identifier] isEqualToString:@"date"]){
        [[tableColumn headerCell] setTitle:@"Date"];
        return [t date];
    }
    if ([[tableColumn identifier] isEqualToString:@"type"]){
        [[tableColumn headerCell] setTitle:@"Type"];
        return [t type];
    }
    if ([[tableColumn identifier] isEqualToString:@"category"]){
        [[tableColumn headerCell] setTitle:@"Category"];
        return [t category];
    }
    if ([[tableColumn identifier] isEqualToString:@"amount"]){
        [[tableColumn headerCell] setTitle:@"Amount"];
        return [NSString stringWithFormat:@"%f",[t amount]];
    }

    return @"//";
}

-(NSMutableArray *)loadData{
    LoadSave* ls = [LoadSave alloc];
    NSMutableArray* array = [[ls loadDataFromDisk] wallet];
    for (Transaction* t in array){
        //NSLog([t toString]);
    }
    // Load 1 row just to see if I can update the table later with more data
    Transaction *t = [array objectAtIndex:1];
    NSMutableArray * test = [NSMutableArray arrayWithObject:t];
    return test;
}

@结尾

标题:

#import <Foundation/Foundation.h>
#import "Wallet.h"
#import "LoadSave.h"

@interface TableViewController : NSTableView <NSTableViewDataSource,NSTableViewDelegate> {
    NSMutableArray *list;
}

-(void) setData:(NSMutableArray*)data;
-(IBAction)test:(id)sender;
- (id) initWithData:(NSMutableArray*)data;
@end

这是目前我的 AppDelegate:

#import "AppDelegate.h"
#import "Transaction.h"
#import "TableViewController.h"

@implementation AppDelegate

@synthesize tableView;
@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    LoadSave* ls = [LoadSave alloc];
    Wallet *wallet = [ls loadDataFromDisk];
    tableView = [[TableViewController alloc]init];
    [tableView setData: wallet.wallet ];    
}

-(IBAction)test:(id)sender{
    LoadSave* ls = [LoadSave alloc];
    Wallet *wallet = [ls loadDataFromDisk];
    [tableView setData: wallet.wallet ];
}

@end

如您所见,我尝试在应用程序完成启动时和界面上的按钮加载新数据。但什么也没有发生。

编辑2:

这是 AppDelegate 的标题,您可以看到 tableView 是我的控制器类的一个实例:

#import <Cocoa/Cocoa.h>
#import "TableViewController.h"
#import "LoadSave.h"

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property IBOutlet TableViewController *tableView;

-(IBAction)test:(id)sender;

@end

这是 SaveLoad.m 这正是我将用来从任何文件保存、加载和导入数据的内容:

#import "LoadSave.h"

@implementation LoadSave

NSString *const PATH = @"file.csv";

- (NSString *) pathForDataFile
{
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *folder = @".";
    folder = [folder stringByExpandingTildeInPath];

    if ([fileManager fileExistsAtPath:folder] == NO)
    {
        [fileManager createDirectoryAtPath:folder withIntermediateDirectories:NO attributes:nil error:nil];
    }

    NSString *fileName = @"wallet.data";
    return [folder stringByAppendingPathComponent: fileName];
}

- (void)saveDataToDisk:(id)wallet
{
    NSString * path = [self pathForDataFile];

    NSMutableDictionary *rootObject;
    rootObject = [NSMutableDictionary dictionary];

    [rootObject setValue:wallet forKey:@"wallet"];
    [NSKeyedArchiver archiveRootObject:rootObject toFile: path];
}

-(Wallet*)loadDataFromDisk
{
    NSDictionary* root = [NSKeyedUnarchiver unarchiveObjectWithFile:[self pathForDataFile]];
    return [root valueForKey:@"wallet"];
}

-(NSMutableArray*) importDataFromCSV
{
    NSString* contents = [NSString stringWithContentsOfFile:PATH
                                                   encoding:NSUTF8StringEncoding
                                                      error:nil];
    NSArray* lines = [contents componentsSeparatedByString:@"\n"];
    NSRange range;
    range.location = 1;
    range.length = [lines count]-1;
    lines = [lines subarrayWithRange:range];

    NSMutableArray *data = [[NSMutableArray alloc] init];
    for (NSString* line in lines)
    {
        NSArray* fields = [line componentsSeparatedByString:@","];
        Transaction *t = [[Transaction alloc] init:fields[0] transactionType:fields[1] transactionCategory:fields[3] transactionAmount:fields[6]];
        [data addObject:t];
    }
    //    wallet* wallet = [Wallet alloc];
    //    wallet.wallet = data;
    //    LoadSave *ls = [LoadSave alloc];
    //    [ls saveDataToDisk:wallet];
    return data;
}

@end

我猜想没有 init 的 alloc 仍然可以工作,因为我使用的方法就像它们是静态的一样,但是到目前为止我还没有搜索如何使它们成为静态的,所以我现在就这样离开了它们。

与其描述界面,我认为发布它的图片会更容易..

屏幕

不过在这一点上..我可以在这里发布整个项目xD

4

0 回答 0