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

发布于 2024-08-29 09:12:40 字数 1397 浏览 5 评论 0原文

在 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 一起使用?

完成此操作的最简单方法是什么?

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:

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

What is the simplest way to get this done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戒ㄋ 2024-09-05 09:12:40

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

// 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。

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:

If the underlying NSDictionary changes, you do have to reload ([myTable reload] or the like) the UITableView.

邮友 2024-09-05 09:12:40

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

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

这可能比你想要的更多,但这是总体轮廓。

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:

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

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:

radioData = data;
curNode = data.rootNode;

and inside cellForRowAtIndexPath we have:

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

and in 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文