简体   繁体   中英

Custom NSTableCellView

I want to make a NSTableView with customs NSTableCellView, here's what I've done so far :

AppDelegate.h :

#import <Cocoa/Cocoa.h>
#import "TheView.h"

@interface AppDelegate : NSObject <NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate>

@property (weak) IBOutlet NSTableView *tableView;
@property (copy) NSMutableArray *tableContent;
@property (assign) IBOutlet NSWindow *window;

-(IBAction)addRow:(id)sender;

@end

AppDelegate.m :

#import "AppDelegate.h"

@implementation AppDelegate
@synthesize tableContent;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    tableContent = [[NSMutableArray alloc]init];
}

-(id)init{
    self = [super init];

    if (self) {
        tableContent = [[NSMutableArray alloc]init];
    }
    return self;
}

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    NSLog(@"%@",tableContent); //Here tableContent is empty
    return [tableContent count];
}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    NSView *result = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
    TheView *view = [tableContent objectAtIndex:row];
    result = view;
    return result;
}

-(IBAction)addRow:(id)sender {
    TheView *view = [[TheView alloc]init];
    [tableContent addObject:view];
    NSLog(@"%@",tableContent); //Here tableContent result with the correct number of objects
    [_tableView reloadData];
}

But when I try to add an object, nothing happens... TheView is a subclass of NSTableCellView, it's drawing a simple rect. I've of course added NSTableViewDataSource and NSTableViewDelegate.

UPDATE 2 After further researches in my code, I found that in numberOfRowsInTableView , tableContent resulted empty or with the number of object I initialized my array with...

The views your creating seem to have no size. You could try replacing the view initialization, and use initWithFrame instead of init

[[TheView alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];

Also, you can try set in fame on the viewForTableColumn method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM