我在 Mac OS X 10.10 (Storyboard) 中自定义 `NSTableView` 时遇到问题。
我的应用在“MainStoryboard”中包含两个布局,分别是:“ProductViewController”和“DetailViewController”。
在 ProductViewController 布局上,我有一个简单的按钮是“查看详细信息”来更改布局。
DetailViewController
我在布局上自定义了一行,我使用了NSTableCellView
. 但是当 layoutDetailViewController
被调用时,我的数据没有被加载。
我调试了一下,发现它不是调用方法:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// Get a new ViewCell
NSTableCellView *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
// Since this is a single-column table view, this would not be necessary.
// But it's a good practice to do it in order by remember it when a table is multicolumn.
if( [tableColumn.identifier isEqualToString:@"TableColumn"] )
{
ScaryBugDoc *bugDoc = [self.bugs objectAtIndex:row];
cellView.imageView.image = bugDoc.thumbImage;
cellView.textField.stringValue = bugDoc.data.title;
return cellView;
}
return cellView;
}
我实现了(NSTableViewDelegate,NSTableviewDatasource ...)并在自定义单元格和DetailViewController布局之间链接了委托+数据源。
// CustomTableViewController.h
#进口 @interface CustomTableViewController : NSViewController @property (strong) NSMutableArray *bugs; @property (weak) IBOutlet NSScrollView *scrollView; @结尾
// CustomTableViewController.m
#import "CustomTableViewController.h"
#import "ScaryBugDoc.h"
#import "ScaryBugData.h"
#import <Quartz/Quartz.h>
@interface CustomTableViewController ()
@property (weak) IBOutlet NSTableView *bugsTableView;
@property (strong) IBOutlet NSView *viewMain;
@end
@implementation CustomTableViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// Get a new ViewCell
NSTableCellView *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
// Since this is a single-column table view, this would not be necessary.
// But it's a good practice to do it in order by remember it when a table is multicolumn.
if( [tableColumn.identifier isEqualToString:@"TableColumn"] )
{
ScaryBugDoc *bugDoc = [self.bugs objectAtIndex:row];
cellView.imageView.image = bugDoc.thumbImage;
cellView.textField.stringValue = bugDoc.data.title;
return cellView;
}
return cellView;
}
- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn {
NSAlert *alert = [[NSAlert alloc]init];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:@"Password is required."];
[alert runModal];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.bugsTableView.delegate = self;
self.bugsTableView.wantsLayer = TRUE;
}
-(ScaryBugDoc*)selectedBugDoc {
NSInteger selectedRow = [self.bugsTableView selectedRow];
if( selectedRow >=0 && self.bugs.count > selectedRow )
{
ScaryBugDoc *selectedBug = [self.bugs objectAtIndex:selectedRow];
return selectedBug;
}
return nil;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [self.bugs count];
}
@end
是否有一个原因?