如何将按字母顺序排序的NSArray按字母顺序划分为UITableView?

时间:2016-10-25 09:25:11

标签: ios objective-c uitableview

我有一个NSArray self.objects,它是一组用户。

我想将用户名按字母顺序划分为分段UITableView

我拍了这张照片:

NSDictionary *usernameDictionary = [NSDictionary dictionaryWithObject:[self.objects valueForKey:@"username"] forKey:@"usernames"];

我不知道该去哪儿。

更新:

 NSMutableSet *mySet = [[NSMutableSet alloc] init];
    for (NSString *s in [self.objects valueForKey:@"username"] )
    {
        if ( s.length > 0 )
            [mySet addObject:[s substringToIndex:1]];
    }

    NSArray *indexArray = [[mySet allObjects] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

我有章节标题来阅读字母,而在旁边,只有问题是每个部分都有整个数组。并且没有具体针对信件的顺序

很难使用appcoda的numberofrowsinsection示例:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSString *sectionTitle = [animalSectionTitles objectAtIndex:section];
    NSArray *sectionAnimals = [animals objectForKey:sectionTitle];
    return [sectionAnimals count];
}

1 个答案:

答案 0 :(得分:0)

NSArray *sortedArray = [self.objects sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

现在你可以创建一个可变字典,它将按照部分添加对象,即一个键将包含具有相同字母的那些用户名的数组,并且键可以是第一个用户名的第一个字符,并且字母改变。