获取:
*** 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
抱歉,很多代码都是用意大利语编写的,但问题是"国际性的"。为什么我会收到此错误?表数据源也连接到文件的所有者和插座。当运行应用程序而不是显示 5 行和 5 个"hello world"时,显然什么也没发生。帮助
问题可能出在包含表视图的 xib 文件中。是将表视图的委托设置为文件的所有者(这将是 NSApplication 的实例),还是将其委托设置为应用程序委托?需要将其设置为应用程序委托。
如果尚未设置表示应用程序委托的对象(在 UI 布局左侧的边距中可见),则应这样做,并将表视图的 delgate 连接连接到该对象。
使用 Swift 时,问题可能是数据源类没有显式实现 NSTableViewDataSource
协议。特别是,随着 Swift 3 中的 API 重命名,它似乎是导致 Swift 3 语法tableView(_:objectValueFor:row:)
映射到 Objective-C 选择器 tableView:objectValueForTableColumn:row:
的协议。
实现表视图数据源时发现了这一点;尽管我在 Swift 中实现了正确的方法,但我在运行时收到了*** Illegal NSTableView data source
消息。我很困惑,直到我意识到我忘记在我的类声明中包含协议一致性。