2015-02-06 56 views
1

我拼湊了以下代碼來填充表視圖並希望能夠從數組中填充它。 任何人都可以告訴我一些如何?Obj-c OS X:在表視圖中填充項目

- (void)applicationWillTerminate:(NSNotification *)aNotification { 
// Insert code here to tear down your application 
NSLog(@"a"); } 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
NSRect   scrollFrame = NSMakeRect(20, 74, 155, 296); 
NSScrollView* scrollView = [[NSScrollView alloc] initWithFrame:scrollFrame]; 

[scrollView setBorderType:NSBezelBorder]; 
NSRect   clipViewBounds = [[scrollView contentView] bounds]; 
NSTableView* tableView  = [[NSTableView alloc] initWithFrame:clipViewBounds]; 

NSTableColumn* firstColumn  = [[NSTableColumn alloc] initWithIdentifier:@""]; 
[tableView addTableColumn:firstColumn]; 
[tableView setHeaderView:nil]; 
[tableView setDataSource:self]; 
[tableView setRowHeight:40.0]; 
[scrollView setDocumentView:tableView]; 
[[[self window] contentView] addSubview:scrollView]; 

} 

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView 
{ 
    return 5; 
} 

- (id) tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex 
{ 

NSString* cellValue = [NSString stringWithFormat:@"%@ %ld", [aTableColumn identifier], (long)rowIndex]; 

return cellValue; 
} 
+0

你需要添加功能的tableView或只是文本,以細胞的所有單元靜態細胞?並且你在使用筆尖(storyboard)還是xib? – YuviGr 2015-02-06 22:05:05

+0

使用XIB文件。我需要添加的唯一功能是設置每個單元格的背景顏色的能力。 – asimpleman 2015-02-06 22:15:23

+0

故事板允許您完全配置靜態表格視圖及其單元格。 – jlehr 2015-02-06 23:41:33

回答

-1

首先,我建議不要使用靜態單元格。爲了成功創建一個帶有靜態單元格的tableView,你需要保持對每個單元格的引用並向其中添加數據,這會在你的代碼中造成一個大混亂。

如果您只想添加更改單元格背景顏色的功能,我推薦使用原型單元格(即您的單元格中包含共同元素)。

但是,如果你堅持要用你需要一個基準保持在的tableView

+0

如果你向下投票添加一些評論,所以我可以修復錯誤的東西... – YuviGr 2015-02-07 00:22:39