如何按字母顺序将NSArray拆分为UITableView部分 [英] How to split NSArray into UITableView sections alphabetically

查看:105
本文介绍了如何按字母顺序将NSArray拆分为UITableView部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用带有节标题的索引表时遇到问题。目前我在右侧有索引,我有正确显示的节标题,标题只显示该节内有数据。

I have having trouble using an indexed table with section headers. Currently I have the indexes down the right hand side and I have the section headers showing correctly, the headers only show if there is data inside of that section.

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

The problem I am having is splitting the NSArray into parts so I can calculate the numberOfRowsInSections correctly. Currently I have the correct number of sections displaying with the correct headers but all of the data is in each section, rather than being split depending on the first letter of the name.

以下是目前的截图:

所有数据都进入每个部分,每部分5行。第(3)节的数量是正确的

All of the data goes into each sections, 5 rows in each. The number of sections (3) is correct

我的代码如下:

- (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拆分为按字母顺序排列的列表以获取节中的正确行。在此先感谢大家!

Any help would be greatly appreciated, I just cant seem to split the NSArray conenctions into a alphabetical list to get the correct rows in a section. Thanks in advance everyone!

推荐答案

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

Where and how are you populating _connections? You're using that array to decide the number of rows per section and to populate those rows, but _connectionsis returning the entire list. You need to split the data in _connections up alphabetically.

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

For example, perhaps you could use an NSMutableArray of NSMutableArrays to group the data by letter. Since you already seem to know how to sort alphabetically, now you just need to identify the first characters of each string to group them properly. To do this, try:

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

(即使第一个字母未知,这种方式仍然有效。)

(This sort would work even if the first letters are unknown.)

然后要获得每个部分的行数,请使用 [sortedConnections objectAtIndex:section] 而不是_connections:

Then to get the number of rows per section, use [sortedConnections objectAtIndex:section] instead of _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;
}

这篇关于如何按字母顺序将NSArray拆分为UITableView部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆