我对目标C很陌生,我的代码有点麻烦。我有一个列有世界各国名单的数组。现在,我想根据字母表从这个数组中创建一些字典,然后将所有这些字典放在一个数组中,例如:
这是我的一系列国家:
在这个数组中,我要创建以下内容:
NSArray
NSDictionary{
headerTitle: "A"
rowValue: ("Argentina", "Albania", "Algeria"....etc)
}
NSDictionary {
headerTitle: "B"
rowValue: ("Bahamas", "Bahrain", "Bangladesh"....etc)
}
etc...现在我不能硬编码这一点,因为国家列表不断变化,因为我从一个get服务器获得了我的国家数组。如果你们能告诉我如何从我的国家得到这个字典阵列(按字母顺序排列),我将不胜感激。谢谢。
发布于 2013-10-11 01:06:58
@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发布于 2013-10-11 00:25:16
创建一个节数组:
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];将其添加到适当的节数组中,因此在最后应该有27个键以数组作为对象。希望这能有所帮助!
请注意,没有直接的方法去做它。
发布于 2013-10-11 00:52:46
我很快将这个伪代码组合在一起,但这使用了字典,而不是您所要求的数组。希望能帮上忙。
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;
}https://stackoverflow.com/questions/19308175
复制相似问题