【问题标题】:Filling UITableView Cells from an NSMutableArray within a NSMutableDictionary从 NSMutableDictionary 中的 NSMutableArray 填充 UITableView 单元格
【发布时间】:2011-02-14 14:45:40
【问题描述】:

我有一个包含 NSMutableArrays 的 NSMutableDictionary。字典代表部门,数组代表该部门内的部门。我正在尝试从 NSMutableArray 的内容中填充 UITableView 的单元格。我目前有 UITableView 显示正确数量的部分(部门)和每个部门中正确的单元格数量 [departmentArray 计数];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    // Return the number of sections.

    return [divisionArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    // Return the number of rows in the section.
    NSArray *temp = [divisionDict objectForKey:[divisionArray objectAtIndex:section]];
    return [temp count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    NSString *temp = [divisionArray objectAtIndex:section];
    if ([temp isEqualToString:@"School of Humanities and Social Sciences"]) 
    {
        temp = @"Humanities and Social Sciences";

    } else  if ([temp isEqualToString:@"School of Science and Mathematics"]) {
        temp = @"Science and Mathematics";
    } else  if ([temp isEqualToString:@"School of Education"]) {
        temp = @"Education";
    }
        return temp;
}

我在 cellForRowAtIndexPath 中尝试了许多不同的方法来显示每个部门的部门名称,但我无法让它工作。我知道我必须获取每个键的数组,然后遍历该数组并获取每个部门的名称,但是在 cellForRowAtIndexPath 中实现它让我感到困惑。

任何帮助将不胜感激!

【问题讨论】:

    标签: iphone ios uitableview nsmutablearray nsmutabledictionary


    【解决方案1】:

    我认为您最好将结构更改为包含NSMutableDictionarysNSMutableArray,其中包含NSMutableArrays 的行和NSStrings 的标题。我发现这在我一直在开发的一些代码中非常方便。

    这是如何工作的:

    您有一个NSMutableArray,每个部门都有一个条目,最终成为表格视图中的一个部分。 在数组的每个条目中,您有一个 NSMutableDictionary 包含两个条目,一个是我使用键 @"rows" 的,它包含一个包含行的数组,另一个是我使用键 @"title" 的,即保存部分标题。

    然后你的代码变成:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    {
        // Return the number of sections.
        return [divisionArray count];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    {
        // Return the number of rows in the section.
        NSArray *rowsInSection = [[divisionArray getObjectAtIndex: section] objectForKey: @"rows"];
        return [rowsInSection count];
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
    {
        NSString *rawTitle = [[divisionArray getObjectAtIndex: section] objectForKey: @"title"];
        NSString *sectionTitle = [rawTitle stringByReplacingOccurrencesOfString: @"School of " withString: @""];
        return sectionTitle;
    }
    

    【讨论】:

    • 我明白你的逻辑,但我将如何在单元格中显示每个部门的部门?这就是我坚持的,在 cellForRowAtIndexPath 方法中执行的代码。
    • 要获取单元格中的部门本身,请在 cellForRowAtIndexPath: 方法中从字典中获取它。由于它获取和索引路径,因此您知道部分和行。该部分为您提供字典,其中包含您存储的所有内容。其中之一是带有部门名称的 NSString,另一个是 NSArray 行,您可以从中获取部门的部门。如上面的numberOfRowsInSection:。获得行数组后,您可以像返回计数一样轻松地返回其中的任何一个。
    【解决方案2】:

    保持我的结构不变,我能够弄清楚,这是我在 cellForRowAtIndexPath 方法中放置的行:

    NSArray *temp = [divisionDict objectForKey:[divisionArray objectAtIndex:[indexPath section]]];    
    [[cell textLabel] setText:[temp objectAtIndex:[indexPath row]]];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多