Como dividir o NSArray em seções UITableView em ordem alfabética
Estou tendo problemas para usar uma tabela indexada com cabeçalhos de seção. Atualmente, tenho os índices no lado direito e os cabeçalhos das seções são exibidos corretamente; os cabeçalhos são exibidos apenas se houver dados dentro dessa seção.
O problema que estou tendo é dividir o NSArray em partes para que eu possa calcular o numberOfRowsInSections corretamente. Atualmente, tenho o número correto de seções exibidas com os cabeçalhos corretos, mas todos os dados estão em cada seção, em vez de serem divididos, dependendo da primeira letra do nome.
Aqui está uma captura de tela da aparência atual:
Todos os dados entram em cada seção, 5 linhas em cada. O número de seções (3) está correto
Meu código para isso é o seguinte:
- (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];
}
}
Qualquer ajuda seria muito apreciada, apenas não consigo dividir as definições do NSArray em uma lista alfabética para obter as linhas corretas em uma seção. Agradecemos antecipadamente a todos!