I have a NSMutableDictionary as below,
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"];
Now,I have to display the keys as section and values as rows in the section,how to do that? please help
In your UITableViewDataSource implementation:
- (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;
}
Notice however that allKeys
and allValues
do not define a specific order (from the docs, "The order of the elements in the array is not defined"). I would convert your structure to an array of arrays before loading the data in the table view.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments