简体   繁体   English

NSDictionary可以在iPhone上与TableView一起使用吗?

[英]Can NSDictionary be used with TableView on iPhone?

In a UITableViewController subclass, there are some methods that need to be implemented in order to load the data and handle the row selection event: 在UITableViewController子类中,为了加载数据和处理行选择事件,需要实现一些方法:

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


}

How is NSDictionary supposed to work with TableView? NSDictionary如何与TableView一起使用?

What is the simplest way to get this done? 完成这项工作的最简单方法是什么?

I do not understand why you want to use a dictionary (which is inheritly unordered) for a task that requires answers to ordered questions (rows), but i take it that you have a dictionary already from somewhere and cannot change that. 我不明白为什么你想要一个需要回答有序问题(行)的任务的字典(这是继承无序的),但我认为你已经从某个地方有一本字典而且不能改变它。 If that is the case, you have to define an order you want to display the keys in, thereby deriving an array implicitly. 如果是这种情况,则必须定义要显示键的顺序,从而隐式派生数组。 One way to do this is alphabetically order another one is the following: 一种方法是按字母顺序排列另一种方法如下:

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

value is now the object selected in the case of tableView:didSelectRowAtIndexPath: or the object you need for your cell processing in tableView:cellForRowAtIndexPath: value现在是在tableView:didSelectRowAtIndexPath:中选择的对象,或者是tableView中的单元格处理所需的对象:cellForRowAtIndexPath:

If the underlying NSDictionary changes, you do have to reload ( [myTable reload] or the like) the UITableView. 如果底层的NSDictionary发生了变化,你必须重新加载( [myTable reload]或类似的)UITableView。

Yes. 是。 Here is how we have done it: 我们是如何做到的:

In our xml parser we have this method which loads the xml into a dictionary called dict: 在我们的xml解析器中,我们有这个方法将xml加载到一个名为dict的字典中:

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

And this method 而这种方法

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

} }

In the RadioData class we have an instance variable: 在RadioData类中,我们有一个实例变量:

Node *rootNode;

and a bunch of methods 和一堆方法

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

and a property 和财产

@property (nonatomic) Node *rootNode;

Finally in the ViewController when we init the frame we use: 最后在ViewController中,当我们初始化帧时,我们使用:

radioData = data;
curNode = data.rootNode;

and inside cellForRowAtIndexPath we have: 在cellForRowAtIndexPath里面我们有:

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

and in didSelectRowAtIndexPath: 并在didSelectRowAtIndexPath中:

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

This is probably more than you wanted but that is the general outline. 这可能比你想要的要多,但那是大纲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM