2013-02-19 41 views
-3

將存儲在NSArray中的數據發送到NSTableView並逐行顯示它的最簡單方法是什麼?發送NSArray到NSTableView

例如: 的NSArray有數據[A,B,C]

我希望NSTableView的說:

一個

b

Ç

的NSTableView的只需要1列。

回答

1

您不會將事情「發送」給NSTableView。 NSTableView向你詢問事情。它通過NSTableViewDataSource協議完成。所以你所需要做的就是從這個實現兩個必需的方法(-numberOfRowsInTableView:和-tableView:objectValueForTableColumn:row :),並將tableview的數據源插座連接到你的對象。對於NSTableViewDataSource

文檔是在這裏:https://developer.apple.com/DOCUMENTATION/Cocoa/Reference/ApplicationKit/Protocols/NSTableDataSource_Protocol/Reference/Reference.html

+0

感謝。但我在哪裏實施這些?我如何將NSTableView指向一個對象? – 2013-02-19 18:17:22

+0

在你想要的任何類中(對於簡單的情況,可能是你的應用程序委託),以及你在界面構建器中連接任何其他出口的方式(如果你不知道如何做到這一點,我建議使用Google的基本Cocoa教程。在沒有連接插座的情況下編寫Cocoa應用程序幾乎是不可能的) – 2013-02-19 18:30:16

0

您需要探索的UITableViewDelegate和UiTableViewDataSource委託方法:

#pragma mark --- Table View Delegate Methods ---------------------------- 
//Handles the selection of a cell in a table view 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

//Defines the number of sections in a table view 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

//Defines the header of the section in the table view 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return nil; 
} 

//Defines the number of rows in each section 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 1; 
} 

//Defines the content of the table view cells 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [myDataArray objectAtIndex:[indexPath row]];//<-pay attention to this line 

    return cell; 
} 
+0

剛剛意識到我的答案涉及到UIKit,而不是OSX上使用的任何東西,但我相信這些原則仍然適用。 – 2013-02-19 18:19:04

+0

在某種程度上,這些原則適用於NSTableView,但僅限於基於視圖的模式。 – 2013-02-19 18:31:41