如何按字母顺序将 NSArray 拆分为 UITableView 部分

2023-12-09

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

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

Here is a screenshot of how it currently looks:All of the data goes into each sections, 5 rows in each. The number of sections (3) is correct

所有数据都进入每个部分,每个部分 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 连接拆分为按字母顺序排列的列表以获取部分中的正确行。预先感谢大家!


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

例如,也许您可​​以使用NSMutableArray of NSMutableArrays 按字母对数据进行分组。由于您似乎已经知道如何按字母顺序排序,现在您只需识别每个字符串的第一个字符即可将它们正确分组。为此,请尝试:

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;
}

(即使第一个字母未知,这种排序也可以工作。)

然后要获取每个部分的行数,请使用[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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何按字母顺序将 NSArray 拆分为 UITableView 部分 的相关文章

随机推荐