【问题标题】:Can NSDictionary be used with TableView on iPhone?NSDictionary 可以与 iPhone 上的 TableView 一起使用吗?
【发布时间】:2010-04-17 04:00:49
【问题描述】:

在 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


}

NSDictionary 应该如何与 TableView 一起工作?

完成这项工作的最简单方法是什么?

【问题讨论】:

    标签: iphone objective-c uitableview nsdictionary


    【解决方案1】:

    我不明白您为什么要使用字典(继承无序)来完成需要回答有序问题(行)的任务,但我认为您已经从某个地方获得了字典并且无法更改它。 如果是这种情况,您必须定义要显示键的顺序,从而隐式派生一个数组。一种方法是按字母顺序排列另一种方法如下:

    // 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 现在是在 tableView:didSelectRowAtIndexPath: 的情况下选择的对象,或者是你在 tableView:cellForRowAtIndexPath: 中处理单元格所需的对象:

    如果底层 NSDictionary 发生变化,您必须重新加载([myTable reload] 等)UITableView。

    【讨论】:

    • 你的解决方案对我来说很简单。
    • 简单的解决方案,但这对我帮助很大!
    【解决方案2】:

    是的。以下是我们的做法:

    在我们的 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;
        }
    }
    

    还有这个方法

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

    最后在 ViewController 中初始化我们使用的框架时:

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

    这可能比你想要的要多,但这是大致的大纲。

    【讨论】:

    • 非常感谢您的解决方案。但这对我来说很复杂。
    • 是的......它有点复杂,但这个例子来自一个相当复杂的应用程序。不幸的是,我手头没有更简单的例子。但是,这应该为您提供一个起点。也许使用数组可能更容易。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 2018-02-16
    • 2020-06-30
    相关资源
    最近更新 更多