返回的内容无关紧要,表视图将仅显示.xib文件中手动设置的显示文本(NSTextField的“标题”)。 请帮我弄清楚我做错了什么。感谢。
我的NSTableView只有1列。
以下是代码:
#import "DeviceSettingsController.h"
#import <ImageCaptureCore/ImageCaptureCore.h>
@interface DeviceSettingsController ()
@property (weak) IBOutlet NSTableView *tableViewScanners;
@property (weak) IBOutlet NSMatrix *checkBoxes;
@property NSMutableArray *availableScanners;
@property ICDeviceBrowser *deviceBrowser;
@end
@implementation DeviceSettingsController
- (void)viewDidLoad {
[super viewDidLoad];
_availableScanners = [[NSMutableArray alloc] init];
_deviceBrowser = [[ICDeviceBrowser alloc] init];
_tableViewScanners.delegate = self;
_tableViewScanners.dataSource = self;
_deviceBrowser.browsedDeviceTypeMask = ICDeviceTypeMaskScanner | ICDeviceLocationTypeMaskLocal | ICDeviceLocationTypeMaskShared | ICDeviceLocationTypeMaskBluetooth | ICDeviceLocationTypeMaskBonjour;
_deviceBrowser.delegate = self;
[_deviceBrowser start];
}
- (id)initWithMainView:(NSView *)_theView AndMainController:(NSViewController *)_theViewController AndNibName:(NSString *)nibName AndArgs:(NSArray *)_args {
self = [super initWithMainView:_theView AndMainController:_theViewController AndNibName:nibName AndArgs:_args];
return self;
}
- (void)deviceBrowser:(ICDeviceBrowser *)browser didAddDevice:(ICDevice *)device moreComing:(BOOL)moreComing {
[_availableScanners addObject:device];
[_tableViewScanners reloadData];
}
- (void)deviceBrowser:(ICDeviceBrowser *)browser didRemoveDevice:(ICDevice *)device moreGoing:(BOOL)moreGoing {
[_availableScanners removeObject:device];
[_tableViewScanners reloadData];
}
- (void)didRemoveDevice:(ICDevice *)device {
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [_availableScanners count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSString *retVal = [((ICScannerDevice *)[_availableScanners objectAtIndex:row]) name];
return retVal;
}
@end
答案 0 :(得分:8)
tableview内容模式是基于单元格还是基于视图?我假设它是基于视图的,因为你提到了一个xib,因此你必须使用
而不是objectValueForTableColumn(基于单元格)- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
另见Apple Docs,所以典型的实现方式如下
- (NSView *)tableView:(NSTableView *)tableView
viewForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row {
// Retrieve to get the @"MyView" from the pool or,
// if no version is available in the pool, load the Interface Builder version
NSTableCellView *result = [tableView makeViewWithIdentifier:@"MyView" owner:self];
// Set the stringValue of the cell's text field to the nameArray value at row
result.textField.stringValue = [self.nameArray objectAtIndex:row];
// Return the result
return result;
}