0

我在 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

是否有一个原因?

4

2 回答 2

0

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

表视图控制器.h

#import <Cocoa/Cocoa.h>

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

表视图控制器.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

谢谢您的支持。

于 2015-03-13T14:47:35.407 回答
0

您必须确保已调用该reloadData方法

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTableView_Class/#//apple_ref/occ/instm/NSTableView/reloadData

于 2015-02-26T09:44:58.493 回答