. 1 2 . 3 // sort initials packet array . 4 . 5 - (NSMutableArray *) sortObjectsAccordingToInitialWith: (NSArray * ) {ARR . 6 . 7 . 8 . 9 // initialize UILocalizedIndexedCollation 10 . 11 UILocalizedIndexedCollation collation * = [UILocalizedIndexedCollation currentCollation]; 12 is 13 is 14 15 / / stars collation index number, here is 27 (a 26 letters and #) 16 . 17 NSInteger sectionTitlesCount = [[collation sectionTitles] COUNT]; 18 is . 19 //NewSectionsArray initialize an array used to store the resultant data, the data model we should eventually get the form @ [@ [In the beginning of the data array A], @ [B to the beginning of the data array], @ [C at the beginning of the data array], @ ... [# in data array (other) at the beginning]] 20 is 21 is NSMutableArray newSectionsArray * = [[NSMutableArray the alloc] initWithCapacity: sectionTitlesCount]; 22 is 23 is 24 25 // initializes an empty array 27 was added newSectionsArray 26 is 27 for (index NSInteger = 0 ; index <sectionTitlesCount; index ++ ) { 28 29 NSMutableArray Array * = [[NSMutableArray the alloc] the init]; 30 31 is } 32 33 is 34 is 35 // each section under a name assigned to 36 37 [ for (* ContactsUserModel usermodel in _aryUser) { 38 is 39 // position where the value of the acquired name attribute, such as "Lin", the first letter is L, A ~ Z in the ranked 11 (the first bit is 0), sectionNumber to 11 40 41 is // attribute name trueName model corresponding to 42 is 43 is 44 is 45 NSInteger sectionNumber = [collation sectionForObject: usermodel collationStringSelector: @selector (trueName)]; 46 is 47 // the name of "lindane" in the p-added newSectionsArray an array of 11 to 48 49 NSMutableArray * sectionNames = newSectionsArray [sectionNumber]; 50 51 is [sectionNames addObject: usermodel]; 52 is 53 is 54 is 55 56 is 57 is NSLog ( @ " sectionNames ===% @ " , sectionNames); 58 59 60 61 is } 62 is 63 is 64 65 NSLog ( @ " newSectionsArray ==% @ " , newSectionsArray) ; 66 67 68 69 70 71 is // Sort the array for each section in accordance with the attribute name 72 73 is for (index NSInteger = 0 ; index <sectionTitlesCount; index ++ ) { 74 75 NSMutableArray *personArrayForSection = newSectionsArray[index]; 76 77 NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)]; 78 79 newSectionsArray[index] = sortedPersonArrayForSection; 80 81 } 82 83 NSLog(@"newSectionsArray==%@",newSectionsArray); 84 85 86 87 // //删除空的数组 88 89 NSMutableArray *finalArr = [NSMutableArray new]; 90 91 _sectionTitles =[NSMutableArray array]; 92 93 [_sectionTitles addObject:@"部门"]; 94 95 96 97 98 99 for (NSInteger index = 0; index < sectionTitlesCount; index++) { 100 101 102 103 if (((NSMutableArray *)(newSectionsArray[index])).count != 0) { 104 105 [finalArr addObject:newSectionsArray[index]]; 106 107 [self.sectionTitles addObject: [[collation sectionTitles] objectAtIndex: index]]; 108 109 110 111 112 113 } 114 115 } 1 16 117 1 18 119 return finalArr; // delete no array value 120 121 122 123 124 125 / / return newSectionsArray; 126 127 128 129 } 130. 131 is 132 134 135 #pragma Mark SectionTitles 136 137 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 138 139 { 140 141 return self.sectionTitles[section]; 142 143 } 144 145 146 147 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 148 149 { 150 151 return self.sectionTitles; 152 153 }