Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa NSView in NSTableView Cell

I'm new to cocoa and I'm getting frustrated, I've spent almost half the day trying to find out how to add an NSView to a NSTableView cell, but I haven't found a nice guide that can help me do what I would like to achieve, maybe someone can have a look at what I've tried and tell me why it's not working and how I could get it to work...

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSTableCellView *view = [tableView makeViewWithIdentifier:@"MyView" owner:self];
NSTextField *textfield = [[NSTextField alloc]initWithFrame:NSMakeRect(0, 0, 100, 30)];
[textfield setStringValue:predictate_search[row]];
[textfield setBackgroundColor:[NSColor redColor]];
[view addSubview:textfield];
[view setNeedsDisplay:YES];
return view;
}

What I would like to achieve is to have two NSTextFields above each other and the table cell to have a custom background. The above is me just trying to get one NSTextField to work, but with no luck...

The NSTableView is being created programmatically:

NSScrollView *scrollView = [[NSScrollView alloc]initWithFrame:bg];
    [scrollView setHasVerticalScroller:YES];
    [self addSubview:scrollView];

    search_results = [[NSTableView alloc]initWithFrame:bg];
    NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"id"];
    [[column headerCell] setStringValue:@"Cities"];
    [column setWidth:1000.0];

    [search_results addTableColumn:column];
    [search_results setDelegate:(id)self];
    [search_results setDataSource:(id)self];
    [search_results reloadData];

    [scrollView setDocumentView:search_results];

I'm slightly confused what to put for the makeViewWithIdentifier:, I've watched the WWDC 2011 video on NSTableViews but I'm still not really sure.

If you require more information please ask

Thanks

EDIT After first answer:

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSTableCellView *view = [tableView makeViewWithIdentifier:[tableColumn identifier] owner:self];
if(view == nil){
    NSTableCellView *view = [[NSTableCellView alloc]initWithFrame:[tableView frame]];
    view.identifier = [tableColumn identifier];
}
NSTextField *textfield = [[NSTextField alloc]initWithFrame:NSMakeRect(0, 0, 100, 30)];
[textfield setStringValue:predictate_search[row]];
[textfield setBackgroundColor:[NSColor redColor]];
[view addSubview:textfield];
[view setNeedsDisplay:YES];
return view;

}

However it is still not working?

like image 802
Lennart Avatar asked Nov 11 '12 18:11

Lennart


1 Answers

The code below solved my problem:

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSView *view = [tableView makeViewWithIdentifier:@"MyView" owner:self];

if (view == nil) {
    view = [[NSView alloc]initWithFrame:NSMakeRect(0, 0, 100, 30)];
    NSTextField *result = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 25, 800, 25)];
    NSTextField *result2 = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 800, 25)];
    result.stringValue = [predictate_search objectAtIndex:row];
    result2.stringValue = @"UnitedKingdom";
    [view addSubview:result];
    [view addSubview:result2];
    [view setNeedsDisplay:YES];
}

return view;

}

Thanks for helping :)

like image 66
Lennart Avatar answered Dec 14 '22 22:12

Lennart