NSTableView无法更新可视化数据

时间:2012-11-25 20:56:20

标签: macos cocoa nstableview updating

我知道这是一个典型的问题,我发现许多线索正在讨论这个问题,但我真的无法理解我的实施问题在哪里。 我设置了所有内容,并且能够在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]之后,正在调用

(正如我所料),但事实并非如此。

要明确&#34; setData&#34;放置在扩展NSTableView的TableViewController内部,并实现NSTableViewDataSource。所以&#34;自我&#34;应该是正确的,因为它指的是界面上显示的实际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;
}

@end

部首:

#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仍然有效,因为我使用的方法就像它们是静态的一样,但是到目前为止我还没有搜索过如何使它们静止,所以我把它们留给了它们现在。

而不是描述界面,我认为发布它的图片会更容易..

screen

此时虽然..我可以在这里发布整个项目xD

0 个答案:

没有答案