ios - 每个部分的动态行

标签 ios xcode uitableview nsarray nsdictionary

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

 <__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);

}

最佳答案

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

解析 JSON 后,您将拥有一个 objective-c 集合,调用它:

NSArray *parsedObject;

段数为:
parsedObject.count;

indexPath 处的部分标题为:
parsedObject[indexPath.section][@"EGroupName"]

一节中的行数为:
NSArray *sectionArray = parsedObject[section][@"records"];
[sectionArray count]

给定 indexPath 处的模型项是:
NSArray *sectionArray = parsedObject[indexPath.section][@"records"];
sectionArray[indexPath.row];

编辑 再深入一点,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);
}

关于ios - 每个部分的动态行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38569209/

相关文章:

ios - 获取实际的 UICollectionView 项目间距

ios - 验证 iOS 应用程序上传的警告和错误

ios - 如何使 UIScrollView 内的 UIScrollView 同时 Action

swift - Xcode 8.2 Swift 编译器错误 : "Segmentation Fault: 11"

ios - Xcode - Bot 在状态 15 失败后太长时间没有输出

ios - 如何计算每个部分中的动态行数

ios - 为 Facebook 应用程序构建模拟器

iphone - 当有多个 NSURLConnection 时,如何确定哪个 NSURLConnection 完成了加载?

ios - UITableView 和 UIButton 重叠而不考虑单元格高度

ios - 在 iOS 中使用 NSURLConnection 获取分页 URL 数据