一系列词典?

时间:2013-10-11 00:00:41

标签: objective-c arrays nsdictionary

我对目标c很新,我的代码遇到了一些麻烦。我有一个数组,其中包含世界各国的列表。现在我想根据字母表从该数组中创建一些字典,然后将所有这些字典放在一个数组中,例如:

这是我的一系列国家:

  • “阿富汗”
  • “阿尔巴尼亚”
  • “阿尔及利亚”
  • “安哥拉”
  • “阿根廷”
  • “巴哈马”
  • “古巴”
  • “丹麦”

从那个数组我要创建它:

          NSArray


          NSDictionary{
                        headerTitle: "A"
                        rowValue: ("Argentina", "Albania", "Algeria"....etc)
                       }


          NSDictionary {
                        headerTitle: "B"
                        rowValue: ("Bahamas", "Bahrain", "Bangladesh"....etc)
                       }

                      etc...

现在我无法对此进行硬编码,因为自从我从网络服务器获取我的一系列国家/地区后,国家/地区列表不断变化。如果你们能告诉我如何从我的一系列国家获得这个字典数组(按字母顺序排列),我将不胜感激。谢谢。

3 个答案:

答案 0 :(得分:0)

创建一个剖面数组:

NSArray* sectionArray = [NSArray arrayWithArray: [@"A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|#" componentsSeparatedByString:@"|"]];

取第一个字母,在使用子字符串之前使用检查:

NSString *firstLetter = [strValue substringToIndex:1];

添加正确的section数组,所以最后你应该有27个Dict,数组作为对象。希望这可以帮助!

请注意,没有直接的方法可以做到。

答案 1 :(得分:0)

我很快将这个伪代码放在一起,但这会使用字典而不是您请求的数组。希望它有所帮助。

    NSMutableDictionary *countryFirstLetters = [NSMutableDictionary dictionary];
    for (NSString *countryName in countries)
    {
        NSString *firstLetter = [[countryName substringToIndex:1] uppercaseString];
        NSMutableDictionary *firstLetterDict = countryFirstLetters[firstLetter];
        if (!firstLetterDict)
        {
            firstLetterDict = [NSMutableDictionary dictionary];
        }
        firstLetterDict[@"headerTitle"] = firstLetter;

        NSMutableString *rowValueString = firstLetterDict[@"rowValue"];
        if (!rowValueString)
        {
            rowValueString = [[NSMutableString alloc] initWithString:countryName];
        }
        else
        {
            [rowValueString appendFormat:@", %@", countryName];
        }
        firstLetterDict[@"rowValue"] = rowValueString;

        countryFirstLetters[firstLetter] = firstLetterDict;
    }

答案 2 :(得分:0)

@implementation NSArray (CountryNamesArray)

+(NSArray*)sectionsArrayFromCountryNames:(NSArray*)countryNames
{
        NSMutableDictionary * countriesDict = [ NSMutableDictionary dictionary ] ;

        for( NSString * countryName in countryNames )
        {
            NSString * firstLetter = [ [ countryName substringToIndex:1 ] uppercaseString ] ;
            NSDictionary * countryDict = countriesDict[ firstLetter ] ;
            if ( ! countryDict ) { countryDict = @{ @"headerTitle" : firstLetter, @"rowValues" : [ NSMutableArray array ] }; } ;

            [ countryDict[@"rowValues"] addObject:countryName ] ;
        }

        return [ countriesDict allValues ] ;
}

@end

这是NSArray上的一个类别,可以满足您的需求。

像这样使用

NSArray * countriesArray = [ NSArray sectionsArrayFromCountryNames:<#countryNames#> ];

但是,我认为更好的方法可能是拥有一个已排序的节名称数组,而节名称是字典中的键,其值是每个节的行。像这样:

// protocol all your cells will support
@protocol TableViewCell
@property ( nonatomic, strong ) id value ;  // value cell should display in table
+(NSString*)reuseIdentifier ;
+(instancetype)cell ;
@end

@interface TableViewCell : UITableViewCell<TableViewCell>
@end

@implementation TableViewCell

@synthesize value = _value ;

-(void)setValue:(id)value
{
    _value = value ;
    self.textLabel.text = value ;
}

+(NSString*)reuseIdentifier
{
    return NSStringFromClass( self ) ;
}

+(instancetype)cell
{
    return [[ [ self class ] alloc ] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[ self reuseIdentifier ] ] ;
}

@end

@interface TableViewDataSource ()
@property ( nonatomic, strong ) NSArray * cityNames ;
@property ( nonatomic, strong ) NSArray * sectionTitles ;
@property ( nonatomic, strong ) NSMutableDictionary * sectionsDictionary ;
@end

@implementation TableViewDataSource

-(void)setCityNames:(NSArray*)names
{
    NSMutableSet * sectionTitles = [ NSMutableSet set ] ;
    NSMutableDictionary * newSectionsDict = [ NSMutableDictionary dictionary ] ;

    for( NSString * cityName in names )
    {
        NSString * sectionTitle = [[ cityName substringToIndex:1 ] uppercaseString ] ;
        [ sectionTitles addObject:sectionTitle ] ;
        NSMutableArray * sectionRows = newSectionsDict[ sectionTitle ] ;
        if ( !sectionRows )
        {
            sectionRows = [ NSMutableArray arrayWithObject:cityName ] ;
            newSectionsDict[ sectionTitle ] = sectionRows ;
        }
        else
        {
            [ sectionRows addObject:cityName ] ;
        }
    }

    self.sectionTitles = [[ sectionTitles allObjects ] sortedArrayUsingSelector:@selector( compare: ) ] ;
    self.sectionsDictionary = newSectionsDict ;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sectionTitles.count ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [ self.sectionsDictionary[ self.sectionTitles[ section ] ] count ] ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Class<TableViewCell> cellClass = [ TableViewCell class ] ;  // 'cellClass' could change based on type of value to be displayed
    TableViewCell * cell = [ tableView dequeueReusableCellWithIdentifier:[ cellClass reuseIdentifier ] ] ;
    if ( !cell )
    {
        cell = [ cellClass cell ] ;
    }
    cell.value = self.sectionsDictionary[ self.sectionTitles[ indexPath.section ] ][ indexPath.row ] ;
    return cell ;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.sectionTitles[ section ] ;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.sectionTitles ;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return index ;
}

@end
相关问题