Как разбить NSArray на разделы UITableView по алфавиту
У меня возникли проблемы с использованием индексированной таблицы с заголовками разделов. В настоящее время у меня есть индексы внизу справа, и заголовки разделов отображаются правильно, заголовки отображаются только при наличии данных внутри этого раздела.
Проблема, которую я имею, состоит в том, чтобы разбить NSArray на части, чтобы я мог правильно рассчитать numberOfRowsInSections. В настоящее время у меня есть правильное количество разделов, отображаемых с правильными заголовками, но все данные находятся в каждом разделе, а не разбиваются в зависимости от первой буквы имени.
Вот скриншот того, как это выглядит в настоящее время:
Все данные поступают в каждый раздел, по 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 в алфавитный список, чтобы получить правильные строки в разделе. Спасибо всем заранее!