@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