【问题标题】:Dynamic rows for each section每个部分的动态行
【发布时间】:2016-07-25 13:34:04
【问题描述】:

如何计算每个部分的行数并填充它的数据。以下是数据。

 <__NSCFArray 0x7fadb3e73a60>(
    {
        AMID = "";
        AMName = "xyz";
        records =     (
                    {
                EId = 100;
                EMName = "abc";
                EName = "Ename1";
            }
        );
        EGroupId = 001;
        EGroupName = "SectionName1";
        stat = G;
    },

    {
        AMID = "";
        AMName = "zyx";
        records =     (
                    {
                EId = 144;
                EMName = "namename";
                EName = "somestring";
            },
                    {
                EId = 159;
                EMName = "somestring";
                EName = "somestring";
            },
                    {
                EId = 161;
                EMName = "somestring";
                EName = "somestring";
            }

    );
            EGroupId = 86;
            EGroupName = "SectionName2";
            stat = Y;
        }
)

根据上面的数据,我想要部分中的“EGroupName”值并动态计算每个部分的行数(在这种情况下,它是“SectionName1 和 SectionName2”)。 最终输出应该是这样的,

**SectionName1**
    return 1;
**SectionName2**
    return 3;

非常感谢您的帮助。

已编辑

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

  NSString *selectedEngagementGroupId = engagementGroupDataArray[indexPath.section][@"EngagementGroupId"];
  NSString *selectedEngagementId = modelItem[@"EId"];
  NSLog(@"EGId %@",selectedEngagementGroupId);
  NSLog(@"EId %@",selectedEngagementId);

}

【问题讨论】:

    标签: ios xcode uitableview nsarray nsdictionary


    【解决方案1】:

    数组,一旦转换为对象,将是一个字典数组,其中每个字典都包含一个数组,每个字典都包含一个可以用作部分名称的字符串值。对于多节表格视图来说,这是一个非常好的数据源。

    解析完JSON,你会有一个客观的c集合,调用它:

    NSArray *parsedObject;
    

    段数为:

    parsedObject.count;
    

    indexPath 处的section 标题是:

    parsedObject[indexPath.section][@"EGroupName"]
    

    一个section的行数是:

    NSArray *sectionArray = parsedObject[section][@"records"];
    [sectionArray count]
    

    给定 indexPath 处的模型项是:

    NSArray *sectionArray = parsedObject[indexPath.section][@"records"];
    sectionArray[indexPath.row];
    

    EDIT 再深入一点,section 数组本身就是一个字典数组,所以...

    NSArray *sectionArray = parsedObject[indexPath.section][@"records"];
    NSDictionary *modelItem = sectionArray[indexPath.row];
    
    NSNumber *itemId = modelItem[@"EId"];
    NSNumber *itemEMName = modelItem[@"EMName"];
    NSNumber *itemName = modelItem[@"EName"];
    
    // if the table cell is a just a vanilla UITableViewCell...
    cell.textLabel.text = itemName;
    cell.detailTextLabel.text = itemEMName;
    

    再次编辑 请记住,您的所有数据源代码都必须遵循此模式,因此要从数据中获取节级属性...

    NSString *engagementGroupString = engagementGroupDataArray[section][@"EGroupName"];
    NSString *engagementManagerString = engagementGroupDataArray[section][@"AMName"];
    

    再次编辑,再次 您可能会发现每次需要时都将所有这些东西都打出来以获取模型项目很烦人。如果是这样,您可以通过创建一个辅助函数来压缩内容,例如:

    - (NSDictionary *)modelItemForIndexPath:(NSIndexPath *):indexPath {
        NSArray *sectionArray = parsedObject[indexPath.section][@"records"];
        return sectionArray[indexPath.row];
    }
    

    现在,例如,当用户选择某物时:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSString *selectedEngagementGroupId = engagementGroupDataArray[indexPath.section][@"EngagementGroupId"];
        NSDictionary *modelItem = [self modelItemForIndexPath:indexPath];
        NSString *selectedEngagementId = modelItem[@"EId"];
        NSLog(@"EGId %@",selectedEngagementGroupId);
        NSLog(@"EId %@",selectedEngagementId);
    }
    

    【讨论】:

    • 我得到了每个部分的正确行数。如何将“EName”显示为标题,将“EMName”显示为每行的详细信息?
    • @user3310076 - 从 OP 中的数据来看,记录数组是一个字典数组,请参阅编辑以了解如何处理这些。
    • 应用在滚动表格时崩溃。由于未捕获的异常'NSRangeException'而终止应用程序,原因:'-[__NSCFArray objectAtIndex:]:索引(11)超出范围(11)。我将在答案框中发布我的代码。请让我知道我做错了什么。
    • 在哪一行?再看看我的建议,我很有信心这是对的。
    • 可能应该删除该帖子,因为它不是答案。 viewForHeader 代码错误。它应该使用与我上面建议的相同的模式来获取部分数据。见编辑。
    【解决方案2】:

    我假设您知道如何将该 JSON 转换为 Objective-C 集合(您会得到一个我认为的顶级数组对象;但是您没有显示顶级对象,这不是有效的 JSON)。

    您需要创建一个NSMutableDictionary 属性来保存您的数据,键是部分名称,值是关联的records 数组。

    @property NSMutableDictionary *_sections;
    

    然而,一个复杂的问题是字典不维护 order 并且顺序对于UITableView 非常重要,因此您需要将顺序保存在一个单独的数组中:

    @property NSMutableArray *_sectionNames;
    

    将它们分配到解码 JSON 的代码中,然后像这样在其中存储数据:

    NSArray *arrayFromJson = ...;
    for (NSDictionary *dict in arrayFromJson) {
        NSString *sectionName = dict[@"EGroupName"];
        [_sections setObject:dict[@"records"] forKey:sectionName];
        [_sectionNames addObject:sectionName];
    }
    

    例如,如果您想按字母顺序对部分进行排序,那么只需按照您的喜好对_sectionNames 进行排序(读者练习)。

    然后UITableDataSource 方法将是这样的:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return [_sectionNames count];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        NSString *sectioName = _sectionNames[section];
        return [_sections[sectionName] count];
    }
    

    【讨论】:

    • 谢谢。但是,我收到“nsmutabledictionary 没有可见的@interface 声明选择器 addObject”错误。我已经更新了从网络服务收到的实际数据
    • 我撤回了评论,因为 OP 可能已经看到 _sectionNames 属性上的错误,可能将其声明为字典而不是数组。不过,我认为您的编辑改进了答案。
    • 不过,可以通过实现解析结果将是一个字典数组来简化答案,它——无需添加 sectionName 数组——作为数据源就足够了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多