0

我有一个这种类型的 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 , just for the test ”做一个 tableView 不同部分的标题。以及内容“20110330 7Huge Sandstorm over niger”、“20110330 8Huge Sandstorm over niger”、“20110330 9CHuge Sandstorm over niger......”每个部分。

请帮帮我。

4

1 回答 1

2

假设每个键中的子元素是 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
}
于 2012-06-13T15:03:03.160 回答