2012-11-11 47 views
5

我是新來的可可,我越來越沮喪,我花了將近半天的時間試圖找出如何將NSView添加到NSTableView單元格,但是我還沒有找到好指南,可以幫助我做我想實現什麼,也許有人可以看看我已經試過,告訴我爲什麼它不工作,我怎麼能得到它的工作...NSTableView單元格中的可可NSView

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

我想實現的是將兩個NSTextField放在彼此的上方,並使表格單元格具有自定義背景。以上是我只是想獲得一個的NSTextField工作,但沒有運氣...

被編程創建NSTableView的:

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

我稍微困惑放什麼的makeViewWithIdentifier:,我觀看了NSTableViews上的WWDC 2011視頻,但我仍然不確定。

如果您需要了解更多信息請諮詢

感謝

編輯 第一個答案後:

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

}

但是它仍然沒有工作?

回答

1

你的方法返回細胞必須使細胞如果它不是dequeable(因爲不存在)

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

我補充說,但它仍然不工作...... –

+0

只是檢查...你確實實施了其他方法? :: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Protocols/NSTableDataSource_Protocol/Reference/Reference.html –

+0

我已經實現了 - (NSInteger)numberOfRowsInTableView:(NSTableView *) aTableView'據我所知這是唯一的必修... –

6

下面的代碼解決了我的問題:

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

} 

感謝您的幫助:)

0

好吧,只是看着這個,我會添加一個答案,以防萬一有人在未來絆倒了這個。

視圖在代碼片段的第二行中定義。如果它爲零,則它將放入if語句中,並再次定義視圖。來自in語句的視圖消失了,因爲它是在括號範圍內定義的,並在程序離開該範圍時死亡,從而使原始無爲零。

現在可能會有更多的問題,但這一點很突出。我正在對這個問題進行一些研究,發現了這個問題並注意到了範圍問題。