2010-04-17 47 views
6

在一個UITableViewController子類,還有一些方法需要以加載數據和處理行選擇時所要實現的:NSDictionary可以與iPhone上的TableView一起使用嗎?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; //there is only one section needed for my table view 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    
    return [myList count]; //myList is a NSDictionary already populated in viewDidLoad method 
} 

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

    // indexPath.row returns an integer index, 
    // but myList uses keys that are not integer, 
    // I don't know how I can retrieve the value and assign it to the cell.textLabel.text 


    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    // Handle row on select event, 
    // but indexPath.row only returns the index, 
    // not a key of the myList NSDictionary, 
    // this prevents me from knowing which row is selected 


} 

如何NSDictionary中應該具有的TableView工作?

完成這件事最簡單的方法是什麼?

回答

23

我不明白你爲什麼要使用一個字典(這是繼承無序)的任務,需要對有序問題(行)的答案,但我認爲你有一個字典已經從某個地方,不能改變。 如果是這種情況,則必須定義要顯示鍵的訂單,從而隱式導出數組。要做到這一點的方法之一是按字母順序另外一個是:現在

// a) get an array of all the keys in your dictionary 
NSArray* allKeys = [myList allKeys]; 
// b) optionally sort them with a sort descrriptor (not shown) 
// c) get to the value at the row index 
id value = [myList objectForKey:[allKeys objectAtIndex:indexPath.row]]; 

值是的tableView的情況下,選擇對象:didSelectRowAtIndexPath方法:或者你需要在的tableView你的處理對象:的cellForRowAtIndexPath:

如果底層的NSDictionary發生變化,你必須重新加載([myTable reload]或類似的)的UITableView。

+0

您的解決方案對我來說足夠簡單。 – bobo 2010-04-17 05:13:47

+0

簡單的解決方案,但這幫了我很多! – stitz 2010-10-12 19:22:39

3

是的。這裏是如何,我們都做到了:

在我們的XML解析器,我們有這樣的方法,該方法加載XML到一個名爲字典詞典:

-(NSDictionary *)getNodeDictionary:(Node *)node { 
    if (node->level == 0) return xmlData; 
    else { 
     NSDictionary *dict = xmlData; 
     for(int i=0;i<node->level;i++) { 
      if ([[dict allKeys] containsObject:SUBNODE_KEY]) 
       dict = [[dict objectForKey:SUBNODE_KEY] objectAtIndex:*(node->branches+i)]; 
     } 
     return dict; 
    } 
} 

而且這種方法

-(NSDictionary *)getDataForNode:(Node *)node { 
NSDictionary* dict = [[self getNodeDictionary:node] copy]; 
return dict; 

}

在RadioData類中我們有一個實例變量:

Node *rootNode; 

和一堆方法

-(Node *)getSubNodesForNode:(Node *)node; 
-(Node *)getSubNodeForNode:(Node *)node atBranch:(NSInteger)branch; 
-(Node *)getParentNodeForNode:(Node *)node; 
-(NSInteger)getSubNodeCountForNode:(Node *)node; 
-(NSDictionary *)getDataForNode:(Node *)node; 

,並在視圖控制器屬性

@property (nonatomic) Node *rootNode; 

最後,當我們初始化我們使用的框架:

radioData = data; 
curNode = data.rootNode; 

和裏面的cellForRowAtIndexPath我們有:

Node* sub = [radioData getSubNodeForNode:curNode atBranch:indexPath.row]; 
NSDictionary* dets = [radioData getDataForNode:sub];  

和didSelectRowAtIndexPath方法:

Node* node = [radioData getSubNodeForNode:curNode atBranch:indexPath.row]; 
NSDictionary* data = [radioData getDataForNode:node]; 

這比你想可能更多,但是這是大致輪廓。

+0

非常感謝您的解決方案。但對我來說這是一個複雜的問題。 – bobo 2010-04-17 05:11:55

+0

是的......它有點複雜,但例子來自一個相當複雜的應用程序。不幸的是,我手邊沒有一個簡單的例子。但是,這應該給你一個出發點。也許使用數組可能更容易。 – 2010-04-17 05:53:47

相關問題