如何按照字母顺序将NSArray分割成UITableView部分

使用节标题的索引表时遇到问题。 目前我有右边的索引,并且节标题显示正确,标题只显示该部分内部是否有数据。

我遇到的问题是将NSArray分解成部分,所以我可以正确计算numberOfRowsInSections。 目前,我有正确数量的部分显示正确的标题,但所有的数据是在每个部分,而不是根据名称的第一个字母拆分。

这里是它目前的样子的截图: 所有的数据进入每个部分,每个5行。部分(3)的数量是正确的

所有的数据进入每个部分,每个5行。 部分(3)的数量是正确的

我的代码如下:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [firstLetterArray objectAtIndex:section]; } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { NSMutableSet *mySet = [[NSMutableSet alloc] init]; BRConnection *connection = nil; NSMutableArray *firstNames = [[NSMutableArray alloc] init]; for (connection in _connections) { [firstNames addObject:connection.firstName]; } firstNamesArray = firstNames; NSLog(@"%@", firstNamesArray); for ( NSString *s in firstNames) { if ([s length] > 0) [mySet addObject:[s substringToIndex:1]]; } NSArray *indexArray = [[mySet allObjects] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; firstLetterArray = indexArray; return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]; } - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { if ([title isEqualToString:@"{search}"]) { [tableView setContentOffset:CGPointMake(0.0, -tableView.contentInset.top)]; return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index]; } return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ConnectionCell"]; // Display connection in the table cell BRConnection *connection = nil; if (tableView == self.searchDisplayController.searchResultsTableView) { connection = [searchResults objectAtIndex:indexPath.row]; } else { connection = [_connections objectAtIndex:indexPath.row]; } cell.textLabel.text = connection.fullName; cell.textLabel.font = [UIFont fontWithName:@"TitilliumText25L-400wt" size:18]; cell.detailTextLabel.text = connection.company; cell.detailTextLabel.font = [UIFont fontWithName:@"TitilliumText25L-400wt" size:12]; return cell; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. NSUInteger sections = [firstLetterArray count]; return sections; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.searchDisplayController.searchResultsTableView) { return [searchResults count]; } else { return [_connections count]; } } 

任何帮助将不胜感激,我似乎不能将NSArray conenctions拆分成按字母顺序排列的列表,以获得正确的行。 在此先感谢大家!

在哪里以及如何填充_connections ? 您正在使用该数组来确定每个节的行数并填充这些行,但_connections正在返回整个列表。 您需要按字母顺序将_connections的数据拆分。

例如,也许你可以使用NSMutableArrayNSMutableArray来按字母分组数据。 由于您已经知道如何按字母顺序sorting,现在您只需确定每个string的第一个字符即可将其正确分组。 要做到这一点,请尝试:

 NSString *currentPrefix; // Store sortedConnections as a class variable (as you've done with _connections) // so you can access it to populate your table sortedConnections = [[NSMutableArray alloc] init]; // Go through each connection (already ordered alphabetically) for (BRConnection *connection in _connections) { // Find the first letter of the current connection NSString *firstLetter = [connection.fullName substringToIndex:1]; // If the last connection's prefix (stored in currentPrefix) is equal // to the current first letter, just add the connection to the final // array already in sortedConnections if ([currentPrefix isEqualToString:firstLetter]) { [[sortedConnected lastObject] addObject:connection]; } // Else create a new array in sortedConnections to contain connections starting // with this current connection's letter. else { NSMutableArray *newArray = [[NSMutableArray alloc] initWithObject:connection]; [sortedConnections addObject:newArray]; } // To mark this latest array's prefix, set currentPrefix to contain firstLetter currentPrefix = firstLetter; } 

(即使第一个字母是未知的,这种types也是可行的。)

然后为了得到每节的行数,使用[sortedConnections objectAtIndex:section]而不是_connections:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.searchDisplayController.searchResultsTableView) { return [[sortedSearchResults objectAtIndex:section] count]; // hypothetically } else { return [[sortedConnections objectAtIndex:section] count]; } } 

并填充表本质上做相同的使用[sortedConnections objectAtIndex:indexPath.section]

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ConnectionCell"]; // Display connection in the table cell BRConnection *connection = nil; if (tableView == self.searchDisplayController.searchResultsTableView) { connection = [[sortedSearchResults objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; // hypothetically } else { connection = [[sortedConnections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; } cell.textLabel.text = connection.fullName; cell.textLabel.font = [UIFont fontWithName:@"TitilliumText25L-400wt" size:18]; cell.detailTextLabel.text = connection.company; cell.detailTextLabel.font = [UIFont fontWithName:@"TitilliumText25L-400wt" size:12]; return cell; } 

希望能帮到你,我不知道是不是最好的办法,但它的工作原理=)

 NSArray *names = @[@"Ana Carolina", @"Ana carolina", @"Ana luiza", @"leonardo", @"fernanda", @"Leonardo Cavalcante"]; NSMutableSet *firstCharacters = [NSMutableSet setWithCapacity:0]; for( NSString*string in names ){ [firstCharacters addObject:[[string substringToIndex:1] uppercaseString]]; } NSArray *allLetters = [[firstCharacters allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; int indexLetter = 0; NSMutableArray *separeNamesByLetters = [NSMutableArray new]; for (NSString *letter in allLetters) { NSMutableDictionary*userBegeinsWith = [NSMutableDictionary new]; [userBegeinsWith setObject:letter forKey:@"letter"]; NSMutableArray *groupNameByLetters = [NSMutableArray new]; NSString *compareLetter1 = [NSString stringWithFormat:@"%@", allLetters[indexLetter]]; for (NSString*friendName in names) { NSString *compareLetter2 = [[friendName substringToIndex:1] uppercaseString]; if ( [compareLetter1 isEqualToString:compareLetter2] ) { [groupNameByLetters addObject:friendName]; } } indexLetter++; [userBegeinsWith setObject:groupNameByLetters forKey:@"list"]; [separeNamesByLetters addObject: userBegeinsWith]; } NSLog(@"%@", separeNamesByLetters); 

输出:

  ( { letter = A; list = ( "ana carolina", "Ana carolina", "Ana luiza" ); }, { letter = F; list = ( fernanda ); }, { letter = L; list = ( leonardo, "Leonardo Cavalcante" ) } ) 

以防万一你有自定义对象,在@Leo一点修改

 NSMutableSet *firstCharacters = [NSMutableSet setWithCapacity:0]; for( ETUser *user in self.follwings){ [firstCharacters addObject:[[user.name substringToIndex:1] uppercaseString]]; } NSArray *allLetters = [[firstCharacters allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; int indexLetter = 0; NSMutableArray *separeNamesByLetters = [NSMutableArray new]; for (NSString *letter in allLetters) { NSMutableDictionary*userBegeinsWith = [NSMutableDictionary new]; [userBegeinsWith setObject:letter forKey:@"letter"]; NSMutableArray *groupNameByLetters = [NSMutableArray new]; NSString *compareLetter1 = [NSString stringWithFormat:@"%@", allLetters[indexLetter]]; for (ETUser *user in self.follwings) { NSString *compareLetter2 = [[user.name substringToIndex:1] uppercaseString]; if ( [compareLetter1 isEqualToString:compareLetter2] ) { [groupNameByLetters addObject:user]; } } indexLetter++; [userBegeinsWith setObject:groupNameByLetters forKey:@"list"]; [separeNamesByLetters addObject: userBegeinsWith]; } self.follwings = [[NSMutableArray alloc]initWithArray:separeNamesByLetters];