如何自定义NSTableView(在Mac OS X 10.10中使用Storyboard)

时间:2015-02-26 09:16:47

标签: macos nstableview nstablecellview

我在Mac OS X 10.10(故事板)中自定义`NSTableView`时遇到问题。

我的应用程序由“MainStoryboard”中的两个布局组成,有:`ProductViewController`和`DetailViewController`。

在ProductViewController布局上我有一个简单的按钮是"查看详细信息"改变布局。

我在DetailViewController布局上自定义了一行,我使用了NSTableCellView。但是当调用布局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;
}

我实现了(NSTableViewDelegate,NSTableviewDatasource ...)并在自定义单元格和DetailViewController布局之间链接了委托+数据源。

// 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

这是否有原因?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我找到了解决问题的方法。我在使用NSTableView的委托方法时犯了一个错误。 这是我的代码简单,它工作正常。

TableViewController.h

#import <Cocoa/Cocoa.h>

@interface TableViewController : NSViewController <NSTableViewDataSource, NSTableViewDelegate>
@end

TableViewController.m

#import "TableViewController.h"

@interface TableViewController ()

@property (nonatomic) NSMutableArray *iconListData;
@property (nonatomic) NSMutableArray *menuListData;

@end

@implementation TableViewController

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return 6;
}

-(CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {
    return 60;
}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    NSString *identify = [tableColumn identifier];

    self.iconListData = [NSMutableArray arrayWithObjects:image1, image2, image3, image4, image5, image6, nil];
    self.menuListData = [NSMutableArray arrayWithObjects:item1, item2, item3, item4, item5, item6, nil];

    if([identify isEqualToString:@"MainCell"]) {
        NSTableCellView *cellView = [tableView makeViewWithIdentifier:@"MainCell" owner:self];
        cellView.textField.stringValue = self.menuListData[row];
        cellView.imageView.image = [NSImage imageNamed:self.iconListData[row]];
        return cellView;
    }

    return nil;
}

- (void) chooseMenu:(NSString *)name {
    NSAlert *alert = [[NSAlert alloc]init];
    [alert addButtonWithTitle:@"OK"];
    [alert setMessageText:name];
    [alert runModal];
}

- (void)tableViewSelectionDidChange:(NSNotification *)notification {

    NSTableView *tableView = notification.object;
    NSLog(@"User has selected row %ld", (long)tableView.selectedRow);
    NSInteger _row = tableView.selectedRow;
    //NSString *_rowString = [NSString stringWithFormat:@"%li", (long)_row];
    switch (_row) {
        case 0:
            [self chooseMenu:@"Choose row 1"];
            break;
        case 1:
            [self chooseMenu:@"Choose row 2"];
            break;
        case 2:
            [self chooseMenu:@"Choose row 3"];
            break;
        case 3:
            [self chooseMenu:@"Choose row 4"];
            break;
        case 4:
            [self chooseMenu:@"Choose row 5"];
            break;
        case 5:
            [self chooseMenu:@"Choose row 6"];
            break;
        default:
            break;
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
}

@end

感谢您的支持。