2012-11-11 9 views
5

Jestem nowy w kakao i denerwuję się, spędziłem prawie pół dnia próbując dowiedzieć się jak dodać NSView do komórki NSTableView, ale nie znalazłem miły przewodnik, który pomoże mi robić to, co chciałbym osiągnąć, może ktoś może rzucić okiem na to, co próbowałem i powiedz mi, dlaczego to nie działa i jak mogę zmusić go do pracy ...Kakao NSView w komórce NSTableView

-(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; 
} 

Co chciałbym osiągnąć, to posiadanie dwóch NSTextFieldów ponad sobą i komórki tabeli mają niestandardowe tło. Powyższe jest mi po prostu staramy się uzyskać jeden NSTextField do pracy, ale bez powodzenia ...

NSTableView jest tworzona programowo:

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]; 

jestem nieco zdezorientowany, co umieścić na makeViewWithIdentifier:, Oglądałem wideo na NSTableViews, ale nadal nie jestem pewien.

Jeśli potrzebujesz więcej informacji proszę pytać

Dzięki

EDIT Po pierwszej odpowiedzi:

-(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; 

}

Jednak to nadal nie działa?

Odpowiedz

1

metoda, która zwraca komórka musi komórkę jeśli isnt dequeable (bo nikt tam jest)

// There is no existing cell to reuse so we will create a new one 
if (result == nil) { 

    // create the new NSTextField with a frame of the {0,0} with the width of the table 
    // note that the height of the frame is not really relevant, the row-height will modify the height 
    // the new text field is then returned as an autoreleased object 
    result = [[[NSTextField alloc] initWithFrame:...] autorelease]; 

    // the identifier of the NSTextField instance is set to MyView. This 
    // allows it to be re-used 
    result.identifier = @"MyView"; 
    } 
+0

Dodałem, że w, ale nadal nie działa ... –

+0

tylko sprawdzanie ... zaimplementowałeś inne metody? :: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Protocols/NSTableDataSource_Protocol/Reference/Reference.html –

+0

Zaimplementowałem '- (NSInteger) numberOfRowInViewable: (NSTableView *) aTableView' o ile wiem, że to jedyny obowiązkowy ... –

6

Kod poniżej rozwiązać mój 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; 

} 

Dzięki za pomoc :)

0

Po prostu na to spojrzę, dodam odpowiedź na wypadek, gdyby ktoś się o to potykał w przyszłości.

Widok jest zdefiniowany w drugim wierszu fragmentu kodu. Jeśli jest zerowy, przechodzi do instrukcji if, a widok jest zdefiniowany ponownie. Widok, który wychodzi z instrukcji in, znika, ponieważ został zdefiniowany w zakresie nawiasów i umiera, gdy program opuszcza ten zakres pozostawiając pierwotny zero.

Teraz może być o wiele więcej problemów z tym związanych, ale one wystają. Robiłem badania na ten temat i znalazłem to pytanie i zauważyłem problem z zakresu.

Powiązane problemy