Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos NSTableView可以';t更新可视化数据_Macos_Cocoa_Nstableview_Updating - Fatal编程技术网

Macos NSTableView可以';t更新可视化数据

Macos NSTableView可以';t更新可视化数据,macos,cocoa,nstableview,updating,Macos,Cocoa,Nstableview,Updating,我知道这是一个典型的问题,我发现很多线程都在讨论这个问题,但我真的不明白我的实现哪里有问题。 我设置了所有内容,可以在NSTableView中看到加载在NSMutableArray中的数据。问题在于改变这些数据。我所做的是: -(void) setData:(NSMutableArray*)data{ for (int i=0; i<[data count]; i++){ [list addObject:[data objectAtIndex:i]]; } [sel

我知道这是一个典型的问题,我发现很多线程都在讨论这个问题,但我真的不明白我的实现哪里有问题。 我设置了所有内容,可以在NSTableView中看到加载在NSMutableArray中的数据。问题在于改变这些数据。我所做的是:

-(void) setData:(NSMutableArray*)data{
  for (int i=0; i<[data count]; i++){
      [list addObject:[data objectAtIndex:i]];
  }
  [self reloadData];
}
在[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;
}
正如你所看到的,我尝试在应用程序完成启动时加载新数据,并在界面上单击一个按钮。但什么也没发生

编辑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
我想没有init的alloc仍然可以工作,因为我使用的方法就像静态的一样,但我还没有研究如何使它们成为静态的,所以我现在就这样离开了

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


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

你真的非常需要一个
[list removeAllObjects]
-setData:
中的第一行之前,没有任何变化(除了每次都有一个干净的列表),我知道,这就是为什么它是一个注释而不是答案。我只是说,如果它是一个
setXXX:
方法,它在当前的形式下可能不起你所期望的作用。啊,好吧,对不起,我不太习惯这个网站:PAre你确定self是表视图而不是控制器吗?记录self,看看你得到了什么。
#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
#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
#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
#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