this ist not an answer but i have to write a piece of code that is better to read and understand...
My question is:
why is my data array overwritten, and not the Names Array added one by one to my data Array?
-initWithDictionary:(NSDictionary*)theDictionary{
myDictionary = theDictionary;
data = [[NSMutableArray alloc] init];
Names = [[NSMutableArray alloc] init];
NSDictionary* tempDict = [[NSDictionary alloc] init];
NSString *currentFamilyName = [[NSString alloc] init];
NSString *currentName = [[NSString alloc] init];
NSString *currentPhoneNumber = [[NSString alloc] init];
NSString *currentEmail = [[NSString alloc] init];
NSString* currentCategory = @"a"; //set the First Category (A - Z)
for (NSDictionary *anyID in [[[myDictionary objectForKey:@"ifeed"]objectForKey:@"channel"] objectForKey:@"item"]) {//For Any Item in XML Dictionary
//FamilyName and Name do always exist
currentFamilyName = [NSString stringWithString:[[[anyID objectForKey:@"n"]objectForKey:@"family-name"] objectForKey:@"content"]];
currentName = [NSString stringWithString:[[[anyID objectForKey:@"n"]objectForKey:@"given-name"] objectForKey:@"content"]];
currentPhoneNumber = [[NSString alloc] initWithString:@""]; //we dont want the last phoneNumber
currentEmail = [[NSString alloc] initWithString:@"keine Email hinterlegt"]; //we dont want the last email address
for (NSDictionary *currentDict in [anyID objectForKey:@"tel"]){ //Current XML Item in my Dictionary -> telephone number Element "tel"
if ([[[currentDict objectForKey:@"attributes"] objectForKey:@"type"] isEqualToString:@"work"]) {//if tel has the attribute "work"
currentPhoneNumber = [currentDict objectForKey:@"content"]; //set items phone Number
}
}
if ([[anyID objectForKey:@"email"] objectForKey:@"content"] != nil){
currentEmail = [currentEmail stringByAppendingString:[[anyID objectForKey:@"email"] objectForKey:@"content"]];
}
if ([[[currentFamilyName substringToIndex:1] lowercaseString] isEqualToString:[currentCategory lowercaseString]] == NO && Names != nil){
//if the first Letter of the Name is not equal to the Category (A - Z)
NSLog(@"adding the category-section:%@ with %d items to data", currentCategory, [Names count]);
[self addDataSectionWithCategory:currentCategory]; //add a new Names Section to the data Array
currentCategory = [currentFamilyName substringToIndex:1]; //set last Category
NSLog(@"changed Category to: %@", currentCategory);
}
tempDict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: currentFamilyName, currentName, currentPhoneNumber, currentEmail, nil]
forKeys:[NSArray arrayWithObjects: @"name", @"vorname", @"nummer", @"email", nil]];
[Names addObject:tempDict];
[tempDict release];
}
return self;
}
-(void)addDataSectionWithCategory:(NSString*)cat_{ //add one Section to data Array - Sections should be displayed in the UITableView
//if you log Names here it all will be good. Names will have the correct items.
[data addObject: [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: cat_, self.Names, nil]
forKeys:[NSArray arrayWithObjects: @"header", @"data", nil]]]; //data seems to be overwritten
[Names removeAllObjects]; //clear Name for the next section (A - Z) of "Names"
}