首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSDictionary in tableView

NSDictionary in tableView
EN

Stack Overflow用户
提问于 2012-06-13 14:55:14
回答 1查看 740关注 0票数 0

我有一个这种类型的NSDictionary:

代码语言:javascript
复制
" **********" =     (
    "20110330 7Huge Sandstorm over niger",
    "20110330 8Huge Sandstorm over niger",
    "20110330 9CHuge Sandstorm over niger",
    "20110330 AHuge Sandstorm over niger",
    "20110330 B10CHuge Sandstorm over niger",
    "20110330 **CHuge Sandstorm over niger",
    ""
);
" 2012" =     (
    "20110330 Huge Sandstorm over niger",
    ""
);
" just for the test" =     (
    "20110330 AHuge Sandstorm over niger",
    "20110330 BHuge Sandstorm over niger",
    "20110330 CHuge Sandstorm over niger",
    "20110330 1Huge Sandstorm over niger",
    "20110330 2Huge Sandstorm over niger",
    "20110330 3Huge Sandstorm over niger",
    "20110330 4Huge Sandstorm over niger",
    "20110330 5CHuge Sandstorm over niger",
    "20110330 6Huge Sandstorm over niger",
    ""
);

我想做一个tableView,带有“*,2012,只是为了测试”不同部分的标题。“尼日尔20110330 7雨格沙尘暴”、“尼日尔上空20110330 8雨格沙尘暴”、“尼日尔20110330 9呼热沙尘暴……”每一节。

请帮帮我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-13 15:03:03

假设每个键中的子元素是NSArrays,那么您可以返回字典中的键数作为节数,每个节的标题的实际键,然后对于每个节中的行数,返回数组中在该特定键下的元素数。

代码语言:javascript
复制
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [myDictionary count];//returns the number of key/object pairs
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSArray *allKeys = [myDictionary allKeys];
    return [allKeys objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *allKeys = [myDictionary allKeys];
    NSString *curKey = [allKeys objectAtIndex:section];
    NSArray *curArray = [myDictionary objectForKey:curKey];

    return [curArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //normal cell acquisition code

    NSArray *allKeys = [myDictionary allKeys];
    NSString *curKey = [allKeys objectAtIndex:indexPath.section];
    NSArray *curArray = [myDictionary objectForKey:curKey];
    NSString *curValue = [curArray objectAtIndex:indexPath.row];
    //Do something with the string
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11017639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档