2015-08-08 100 views
0
contactSectionDict = [[NSMutableDictionary alloc] init]; 
contactsForSection = [[NSMutableArray alloc] init]; 
contactsIndexTitles = @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H",@"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z",@"#"]; 

for(int i=0;i<contactsIndexTitles.count;i++) 
{ 
    for(int j=0;j<names.count;j++) 
    { 
     NSArray * splitArray = [[names objectAtIndex:j] componentsSeparatedByString:@" "]; 
     if ([[splitArray objectAtIndex:0] hasPrefix:[contactsIndexTitles objectAtIndex:i]]) { 
      //[contactSectionDict setValue:[names objectAtIndex:j] forKey:[contactsIndexTitles objectAtIndex:i]]; 
      [contactsForSection addObject:[names objectAtIndex:j]]; 
     } 
    } 
    [contactSectionDict setValue:contactsForSection forKey:[contactsIndexTitles objectAtIndex:i]]; 
    [contactsForSection removeAllObjects]; 
} 

在第一次迭代中,它存儲contactsForSection中的36個對象,並將其存儲在contactsectionDict中,用於鍵「A」,然後我想存儲索引B的聯繫人,爲此我從contactsForSection中刪除對象,但它也刪除了值字典中的關鍵字「A」。 當字典將設置對所有索引,然後我會給它的表視圖顯示他們像蘋果聯繫應用程序。 請建議我更好的方法。我的NSMutableDictionary刪除對象當我刪除I​​OS中的NSMutableArray?

回答

1

您需要創建一個新的contactsForSection陣列的每個字母: