So teilen Sie NSArray alphabetisch in UITableView-Abschnitte auf
Ich habe Probleme bei der Verwendung einer indizierten Tabelle mit Abschnittsüberschriften. Momentan habe ich die Indizes auf der rechten Seite und die Abschnittsüberschriften werden korrekt angezeigt. Die Überschriften werden nur angezeigt, wenn sich Daten in diesem Abschnitt befinden.
Das Problem, das ich habe, ist das Aufteilen des NSArray in Teile, damit ich die numberOfRowsInSections richtig berechnen kann. Momentan wird die richtige Anzahl von Abschnitten mit den richtigen Überschriften angezeigt, aber alle Daten befinden sich in jedem Abschnitt, anstatt in Abhängigkeit vom ersten Buchstaben des Namens aufgeteilt zu werden.
Hier ist ein Screenshot, wie es aktuell aussieht:
Alle Daten gehen in Abschnitte mit jeweils 5 Zeilen. Die Anzahl der Abschnitte (3) ist korrekt
Mein Code dafür ist wie folgt:
- (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];
}
}
Jede Hilfe wäre sehr dankbar. Ich kann die NSArray-Verbindungen nicht in eine alphabetische Liste aufteilen, um die richtigen Zeilen in einem Abschnitt zu erhalten. Vielen Dank im Voraus an alle!