【发布时间】:2016-03-25 21:42:36
【问题描述】:
我是 iOS 新手,正在开发一个应用程序。在这个我有一个包含标题的数组(a,b,c,....)作为值,在另一个数组中我有一个包含动物名称作为字符串的值,现在我想将这些值作为键值存储在字典,意思是动物的名字以“a”开头应该像这样添加,
NSDictionary *animals;
NSArray *animalSectionTitles;
//Edit by me
NSMutableDictionary *anim;
NSArray *animTitles;
NSMutableArray *commmonary;
NSMutableArray *myary;
}
@end
@implementation AnimalTableTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
anim = [[NSMutableDictionary alloc]init];
// animals = @[@"Bear", @"Black Swan", @"Buffalo", @"Camel", @"Cockatoo", @"Dog", @"Donkey", @"Emu", @"Giraffe", @"Greater Rhea", @"Hippopotamus", @"Horse", @"Koala", @"Lion", @"Llama", @"Manatus", @"Meerkat", @"Panda", @"Peacock", @"Pig", @"Platypus", @"Polar Bear", @"Rhinoceros", @"Seagull", @"Tasmania Devil", @"Whale", @"Whale Shark", @"Wombat"];
animals = @{
@"A" :@[@"Affrican cat", @"Assian cat", @"Alsesian fox"],
@"B" : @[@"Bear", @"Black Swan", @"Buffalo"],
@"C" : @[@"Camel", @"Cockatoo"],
@"D" : @[@"Dog", @"Donkey"],
@"E" : @[@"Emu"],
@"G" : @[@"Giraffe", @"Greater Rhea"],
@"H" : @[@"Hippopotamus", @"Horse"],
@"K" : @[@"Koala"],
@"L" : @[@"Lion", @"Llama"],
@"M" : @[@"Manatus", @"Meerkat"],
@"P" : @[@"Panda", @"Peacock", @"Pig", @"Platypus", @"Polar Bear"],
@"R" : @[@"Rhinoceros"],
@"S" : @[@"Seagull"],
@"T" : @[@"Tasmania Devil"],
@"W" : @[@"Whale", @"Whale Shark", @"Wombat"]};
commmonary = [NSMutableArray arrayWithObjects:@"Bear", @"Buffalo",@"Camel", @"Cockatoo",@"Dog",@"Donkey",@"Emu",@"Giraffe",@"Hippopotamus", @"Horse",@"Koala",@"Lion",@"Llama",@"Lion", @"Llama",@"Manatus",@"Meerkat",@"Panda",@"Peacock",@"Pig", @"Platypus",@"Rhinoceros",@"Seagull",@"Seagull",@"Whale",@"Wombat", nil];
animalSectionTitles = [[animals allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
//adding alll keys in array from dictionary
animTitles = [animals allKeys];
NSLog(@"===My all keys for dictionary----%@",animTitles);
NSLog(@"===my anim common aray is---%li",commmonary.count);
//new logic..!
for(NSString *letter in animTitles)
{
[anim setObject:[NSMutableArray array] forKey:letter];
}
for(NSString *animal in commmonary)
{
NSString *firstLetter = [animal substringToIndex:1];
NSMutableArray *arr = [anim objectForKey:firstLetter];
[arr addObject:animal];
[anim setObject: arr forKey: firstLetter];
NSLog(@"==my array is==%@",anim);
}
}
//new logic..!!
- (NSString *)getImageFilename:(NSString *)animal
{
NSString *imageFilename = [[animal lowercaseString] stringByReplacingOccurrencesOfString:@" " withString:@"_"];
imageFilename = [imageFilename stringByAppendingString:@".jpg"];
return imageFilename;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [animTitles count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSString *sectionTitle = [animTitles objectAtIndex:section];
NSArray *sectionAnimals = [anim objectForKey:sectionTitle];
return [sectionAnimals count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [animalSectionTitles objectAtIndex:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// Configure the cell...
NSString *sectionTitle = [animTitles objectAtIndex:indexPath.section];
NSArray *sectionAnimals = [anim objectForKey:sectionTitle];
NSString *animal = [sectionAnimals objectAtIndex:indexPath.row];
cell.textLabel.text = animal;
cell.imageView.image = [UIImage imageNamed:[self getImageFilename:animal]];
return cell;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
// return animalSectionTitles;
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [animalSectionTitles indexOfObject:title];
}
这里 animTitles 包含 a,c,d...作为值。提前致谢。
【问题讨论】:
标签: ios objective-c arrays dictionary