我在使用带有分组标题的索引表格时遇到了麻烦。目前,我已经将索引放在右侧,并且我已经正确地显示了分组标题,只有在该分组内有数据时才会显示标题。
我遇到的问题是将NSArray拆分成几部分,以便我可以正确计算每个分组中的行数。目前,我显示了正确数量的分组和正确的标题,但所有数据都在每个分组中,而不是根据名称的第一个字母进行分割。
以下是当前的屏幕截图:
所有数据都进入了每个分组,每个分组中有5行。分组数(3)是正确的。
我的代码如下:
任何帮助都将不胜感激,我似乎无法将NSArray conenctions拆分成按字母顺序排列的列表,以获取部分中的正确行。提前感谢大家!
我遇到的问题是将NSArray拆分成几部分,以便我可以正确计算每个分组中的行数。目前,我显示了正确数量的分组和正确的标题,但所有数据都在每个分组中,而不是根据名称的第一个字母进行分割。
以下是当前的屏幕截图:
![All of the data goes into each sections, 5 rows in each. The number of sections (3) is correct](https://istack.dev59.com/SSHTI.webp)
我的代码如下:
- (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拆分成按字母顺序排列的列表,以获取部分中的正确行。提前感谢大家!