【问题标题】:NSDictionary in tableView表视图中的 NSDictionary
【发布时间】:2012-06-13 14:55:14
【问题描述】:

我有一个这种类型的 NSDictionary:

" **********" =     (
    "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",
    ""
);

我想做一个带有“ * , 2012 的 tableView ,只是为了测试”不同部分的标题。而内容“20110330 7Huge Sandstorm over niger”, "20110330 8 尼日尔上空的巨大沙尘暴", “20110330 9CHuge Sandstorm over niger.....”对于每个部分。

请帮帮我。

【问题讨论】:

    标签: iphone objective-c xcode uitableview nsdictionary


    【解决方案1】:

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

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

    【讨论】:

    • 感谢部分和标题在我的测试中很好,但内容没有写在我的 tableView 中。我必须在哪里使用 curValue 变量?
    • curValue 是您将单元格的文本设置为显示的内容。类似cell.textLabel.text = curValue;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 2018-01-17
    相关资源
    最近更新 更多