【发布时间】:2015-02-26 09:16:47
【问题描述】:
我在 Mac OS X 10.10 (Storyboard) 中自定义 `NSTableView` 时遇到问题。
我的应用在 `MainStoryboard` 中包含两个布局,分别是:`ProductViewController` 和 `DetailViewController`。
在 ProductViewController 布局上,我有一个简单的按钮是“查看详细信息”来更改布局。
我在DetailViewController 布局上自定义了一行,我使用了NSTableCellView。但是当 layout DetailViewController 被调用时,我的数据没有被加载。
我调试了一下,发现它不是调用方法:
- (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;
}
我在自定义单元格和 DetailViewController 布局之间实现了(NSTableViewDelegate、NSTableviewDatasource ...)和链接委托+数据源。
// CustomTableViewController.h
#import @interface CustomTableViewController : NSViewController @property (strong) NSMutableArray *bugs; @property (weak) IBOutlet NSScrollView *scrollView; @end
// 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
这是有原因的吗?
【问题讨论】:
标签: macos nstableview nstablecellview