2012-10-08 132 views
6

我想用自定義的NSTableCellViews創建一個NSTableview。自定義NSTableView與自定義NSTableCellView?

這是我現在所擁有的:

  • 的細胞(圖筆尖)nib文件名爲CustomCell.xib
  • 我的電池的自定義類稱爲CustomCell
  • 並且我的代碼AppDelegate.m

H趁着我創造我的表視圖編程:

NSScrollView *tableContainer = [[NSScrollView alloc]initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)]; 
    NSTableView *tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)]; 

    NSTableColumn *firstColumn = [[[NSTableColumn alloc] initWithIdentifier:@"firstColumn"] autorelease]; 
    [[firstColumn headerCell] setStringValue:@"First Column"]; 
    [tableView addTableColumn:firstColumn]; 

    tableView.dataSource = self; 
    tableView.delegate = self; 
    [tableContainer setDocumentView:tableView]; 
    tableContainer.autoresizingMask = NSViewHeightSizable | NSViewMinXMargin; 
    [self.window.contentView addSubview: tableContainer]; 

這裏是委託方法,我希望把我的自定義單元代碼:

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { 


    // In IB the tableColumn has the identifier set to the same string as the keys in our dictionary 
    NSString *identifier = [tableColumn identifier]; 

    if ([identifier isEqualToString:@"myCell"]) { 

     // We pass us as the owner so we can setup target/actions into this main controller object 
     CustomCell *cellView = [tableView makeViewWithIdentifier:identifier owner:self]; 
     // Then setup properties on the cellView based on the column 
     cellView.textField.stringValue = @"Name"; 
     return cellView; 
    } 
    return nil; 
} 

在筆尖文件爲我的自定義單元格我有掛鉤用我的自定義類叫做CustomCell,其中的子類NSTableCellView。至今我還沒有采取其他措施。所以我的CustomCell.m只是默認的初始化代碼。我沒有碰它。而且我沒有在我的nib文件中做任何其他事情,所以我沒有更改文件的所有者或其他任何東西,因爲我不知道該怎麼做。 任何人都可以幫忙嗎?我查看了Apple文檔中的示例文件,但經過幾天的研究,我還沒有找到任何解決方案。如果你能幫助我,我將不勝感激。

+0

你能澄清是什麼問題嗎?我已閱讀你的文章幾次,我無法辨別你有什麼問題。 – FluffulousChimp

+0

我有一個NSTableView,我想要自定義外觀的單元格。所以我建立了一個nib視圖文件,我只有這個NSTableCellView單元。現在,我想以編程方式讓我的NSTableView顯示nib文件中的特定單元格。但是上面的代碼並沒有改變我的表中的任何內容。說得通 ? –

+0

你解決了這個問題嗎?我偶然發現了同樣的問題... – Gossamer

回答

3

這是我落得這樣做:

當然,你必須繼承NSTableCellView並返回它像我一樣的下方。如果你熟悉iOS的表視圖,你應該熟悉類似的方法:

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{ 
    //this will be called first. It will tell the table how many cells your table view will have to display 
    return [arrayToDisplay count]; 

} 

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { 

    //this is called after the count of rows is set. It will populate your table according to the data your array to display contains 

    [tableView setTarget:self]; 
    [tableView setAction:@selector(click)]; 

    NSString *identifier = [tableColumn identifier]; 

    if ([identifier isEqualToString:@"TheCell"]) { 

     CustomCell *cellView = [tableView makeViewWithIdentifier:identifier owner:self]; 
     cellView.cellText.stringValue = [arrayToDisplay objectAtIndex:row]; 
     return cellView; 
    } 

    return nil; 
} 

而當行被選中應該是這樣的所觸發的click方法:

-(void)click{ 

    int index = [table selectedRow]; 

    // Do something with your data 
    //e.g 
    [[arrayToDisplay objectAtIndex:index] findHiggsBoson]; 

} 

而且東西必須添加到NSTableView:

NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"column"]; 
column.width = self.frame.size.width; 
[tableView addTableColumn:column]; 
1

您不需要將NSTableView子類化爲具有自定義NSTableViewCell子類。 您可以考慮使用基於視圖的表視圖也...