每个部分的动态行
Dynamic rows for each section
如何计算每个部分的行数并填充它的数据。下面是数据。
<__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);
}
我假设您知道如何将 JSON 转换为 Objective-C 集合(您将得到一个顶级数组对象,我 认为 ;但是你没有显示顶级对象,这是无效的 JSON).
您将需要创建一个 NSMutableDictionary
属性 来保存您的数据,键将是部分名称,值将是关联的 records
数组。
@property NSMutableDictionary *_sections;
然而,一个复杂的问题是字典不维护 顺序 并且顺序对于 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
进行排序(reader 的练习)。
然后 UITableDataSource
方法将是这样的:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_sectionNames count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *sectioName = _sectionNames[section];
return [_sections[sectionName] count];
}
数组,一旦转换为objects,将是一个字典数组,其中每个字典包含一个数组,每个字典包含一个可用作节名的字符串值。对于 multi-section table 视图来说,这是一个非常好的数据源。
解析 JSON 后,您将得到一个 objective c collection,将其命名为:
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];
编辑 更深入一点,节数组本身就是一个字典数组,所以...
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);
}
如何计算每个部分的行数并填充它的数据。下面是数据。
<__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);
}
我假设您知道如何将 JSON 转换为 Objective-C 集合(您将得到一个顶级数组对象,我 认为 ;但是你没有显示顶级对象,这是无效的 JSON).
您将需要创建一个 NSMutableDictionary
属性 来保存您的数据,键将是部分名称,值将是关联的 records
数组。
@property NSMutableDictionary *_sections;
然而,一个复杂的问题是字典不维护 顺序 并且顺序对于 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
进行排序(reader 的练习)。
然后 UITableDataSource
方法将是这样的:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_sectionNames count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *sectioName = _sectionNames[section];
return [_sections[sectionName] count];
}
数组,一旦转换为objects,将是一个字典数组,其中每个字典包含一个数组,每个字典包含一个可用作节名的字符串值。对于 multi-section table 视图来说,这是一个非常好的数据源。
解析 JSON 后,您将得到一个 objective c collection,将其命名为:
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];
编辑 更深入一点,节数组本身就是一个字典数组,所以...
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);
}