我是 iOS 新手,正在开发一个应用程序。在这个我有一个数组,其中标题包含(a,b,c,....)作为值,在另一个数组中我有一个包含动物名称作为字符串的值,现在我想将此值作为键值存储在字典,意思是动物的名字以“a”开头应该像这样添加,
NSDictionary *animals;
NSArray *animalSectionTitles;
//Edit by me
NSMutableDictionary *anim;
NSArray *animTitles;
NSMutableArray *commmonary;
NSMutableArray *myary;
}
@end
@implementation AnimalTableTableViewController
- (id)initWithStyleUITableViewStyle)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", @"anda", @"eacock", @"ig", @"latypus", @"olar 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"],
@"" : @[@"anda", @"eacock", @"ig", @"latypus", @"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] sortedArrayUsingSelectorselector(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 *)getImageFilenameNSString *)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)numberOfSectionsInTableViewUITableView *)tableView
{
// Return the number of sections.
return [animTitles count];
}
- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
// Return the number of rows in the section.
NSString *sectionTitle = [animTitles objectAtIndex:section];
NSArray *sectionAnimals = [anim objectForKey:sectionTitle];
return [sectionAnimals count];
}
- (NSString *)tableViewUITableView *)tableView titleForHeaderInSectionNSInteger)section
{
return [animalSectionTitles objectAtIndex:section];
}
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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 *)sectionIndexTitlesForTableViewUITableView *)tableView
{
// return animalSectionTitles;
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [animalSectionTitles indexOfObject:title];
}
这里 animTitles 包含 a,c,d...作为值。提前致谢。
Best Answer-推荐答案 strong>
尝试像这样替换你的 for 循环
for (int i=0; i<animTitles.count; i++) {
myary = [[NSMutableArray alloc]init];
for(int j=0;j<commmonary.count;j++){
NSString *a = [commmonary objectAtIndex:i];
NSString *firstLetter = [a substringToIndex:1];
if ([firstLetter isEqualToString: [animTitles objectAtIndex:i]]) {
//adding values to array.
[myary addObject:a];
}
}
[anim setObject: myary forKey: [commmonary objectAtindex:i]];
NSLog(@"====my Dictionary is===%@-->",anim);
}
关于ios - 在ios中添加带有字典键的数组,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/34368186/
|