I have a problem when I custom `NSTableView` in Mac OS X 10.10 (Storyboard).
My app consist of two layout in a `MainStoryboard`, there are: `ProductViewController` and `DetailViewController`.
On ProductViewController layout I have one simple button is "View Detail" to change layout.
I was custom one row onDetailViewController
layout, I used NSTableCellView
. But when layout DetailViewController
is called, my data wasn't loaded.
I debugged and I saw it was not call method:
- (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;
}
I implemented (NSTableViewDelegate, NSTableviewDatasource ...) and linked delegate + datasource between custom cell and DetailViewController layout.
// 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
Is there a reason for this?