2

得到:

*** Illegal NSTableView data source (<NSApplication: 0x101602bc0>).  Must implement numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:

代码.h

    //
//  AppDelegate.h
//  MySQL
//
//  Created by - on 10/12/12.
//  Copyright (c) 2012 - Software. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> {

    NSMutableArray *tabelle_totali;
    IBOutlet NSTableView *tabella_tabelle_totali;
    IBOutlet NSTableView *tabella_contenitore;

}

@property (assign) IBOutlet NSWindow *window;

//Metodo per scaricare dati
- (void) download_tabelle ;
//Manipolazione tabelle ricevute
- (void)tabelle_ricevute:(NSData *)tabelle;
//Refresh tabella
- (IBAction)refresh_tablelle:(id)sender;
//Refresh tabelle
- (int)numberOfRowsInTableView:(NSTableView *)aTableView;
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex;
@end

代码.m

//
//  AppDelegate.m
//  MySQL
//
//  Created by - on 10/12/12.
//  Copyright (c) 2012 Alberto Bellini Software. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [self download_tabelle];
    [tabella_tabelle_totali reloadData];
}

- (void) download_tabelle  {
    NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http://*********************.php"];
    //inizializzazione richiesta url
    NSURL *url = [NSURL URLWithString:databaseURL];
    //Richiesta asincrona per richiedere dati
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *tabelle, NSError *error)
     {
         [self tabelle_ricevute:tabelle];
     }
     ];

}

- (void)tabelle_ricevute:(NSData *)tabelle
{

    NSString *response = [[NSString alloc] initWithData:tabelle encoding:NSUTF8StringEncoding];
    NSArray *tmpResp = [response componentsSeparatedByString:@"####"]; //This array splits the response string
    NSLog(@"%@",response);
    //Aggiungo le mie tabelle al mio array
    [tabelle_totali addObjectsFromArray:tmpResp];

}

- (IBAction)refresh_tablelle:(id)sender {

    //Cancello vecchi dati
    while([[tabella_tabelle_totali tableColumns] count] > 0) {
        [tabella_tabelle_totali removeTableColumn:[[tabella_tabelle_totali tableColumns] lastObject]];
    }

    NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"1"];
    [column setWidth:143];
    [[column headerCell] setStringValue:@"*******"];
    [tabella_tabelle_totali addTableColumn:column];
    [tabella_tabelle_totali reloadData];
}


-(NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
    return 5;
}
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {

    return @"hello world";
}










@end

抱歉,很多代码都是用意大利语编写的,但问题是“国际”。为什么我会收到这个错误?表 dataSource 也连接到文件的所有者和出口。当运行应用程序而不是显示 5 行和 5 个“hello world”时,显然没有任何反应。帮助

4

2 回答 2

3

问题可能出在包含表格视图的 xib 文件中。您是否将表视图的委托设置为文件所有者(这将是 NSApplication 的一个实例),或者您是否将其委托设置为您的应用程序委托?它需要设置为您的应用程序委托。

如果您还没有设置一个代表您的应用程序委托的对象(在 UI 布局左侧的边距中可见),您应该这样做,并将您的表视图的 delgate 连接连接到该对象。

于 2012-12-11T00:53:26.903 回答
0

使用 Swift 时,问题可能是数据源类没有显式实现NSTableViewDataSource协议。特别是,随着 Swift 3 中的 API 重命名,它似乎是导致 Swift 3 语法映射tableView(_:objectValueFor:row:)到 Objective-C 选择器的协议tableView:objectValueForTableColumn:row:

我在实现表视图数据源时发现了这一点;虽然我在 Swift 中实现了正确的方法,但我*** Illegal NSTableView data source在运行时收到了消息。我很困惑,直到我意识到我忘记在我的类声明中包含协议一致性。

于 2016-10-06T17:37:37.797 回答