Ios7 如何在ios的表视图中将字典键显示为节,将值显示为行?
我有一本NSMUTABLE字典,如下所示Ios7 如何在ios的表视图中将字典键显示为节,将值显示为行?,ios7,Ios7,我有一本NSMUTABLE字典,如下所示 NSMutableDictionary *aSite; NSArray *aDocs=[[NSArray alloc]initWithObjects:@"aaaa",@"bbbb",@"cccc", nil]; NSArray *bDocs=[[NSArray alloc]initWithObjects:@"aaaa",@"bbbb",@"cccc", nil]; NSArray *cDocs=[[NSArray alloc]initWithObject
NSMutableDictionary *aSite;
NSArray *aDocs=[[NSArray alloc]initWithObjects:@"aaaa",@"bbbb",@"cccc", nil];
NSArray *bDocs=[[NSArray alloc]initWithObjects:@"aaaa",@"bbbb",@"cccc", nil];
NSArray *cDocs=[[NSArray alloc]initWithObjects:@"aaaa",@"bbbb",@"cccc", nil];
aSite=[[NSMutableDictionary alloc]init];
[aSite setObject:aDocs forKey:@"A library"];
[aSite setObject:bDocs forKey:@"B library"];
[aSite setObject:cDocs forKey:@"C library"];
现在,我必须将键显示为部分,将值显示为部分中的行,如何做到这一点?请帮助您将在NSArray中获得所有钥匙,如:
NSArray*sectionArray =[aSite allKeys];
在此之后,请使用表视图数据源方法设置标题和表行:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return sectionArray.count;
}
及
在这些方法中,您必须有条件地加载单元格。抓取您的“数组字典”,然后:
值
,然后组合一个数组。每个数组表示表视图一个部分的所有行键
,并将它们组合成一个NSString
s数组。将其用作表视图的节名称在UITableViewDataSource实现中:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return aSite.allKeys.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *arrayKey = [aSite.allKeys objectAtIndex:section];
NSArray *a = [aSite objectForKey:arrayKey];
return a.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
NSString *arrayKey = [aSite.allKeys objectAtIndex:indexPath.section];
NSArray *a = [aSite objectForKey:arrayKey];
cell.textLabel.text = [a objectAtIndex:indexPath.row];
return cell;
}
但是请注意,
allKeys
和allValues
没有定义特定的顺序(从文档中可以看出,“数组中元素的顺序没有定义”)。在将数据加载到表视图中之前,我会将您的结构转换为数组。谢谢大家的回答。让我试试。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return aSite.allKeys.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *arrayKey = [aSite.allKeys objectAtIndex:section];
NSArray *a = [aSite objectForKey:arrayKey];
return a.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
NSString *arrayKey = [aSite.allKeys objectAtIndex:indexPath.section];
NSArray *a = [aSite objectForKey:arrayKey];
cell.textLabel.text = [a objectAtIndex:indexPath.row];
return cell;
}