//服务器返回的城市结果大概这样,列举其中一部分,但是显示的时候需要跟手机通讯录一样,abc那种分组的
{
respDesc = "成功",
cities = (
{
siteld = 54,
id = 255,
fid = 76,
node = "阿坝州",
},
{
siteld = 54,
id = 394,
fid = 76,
node = "巴中市",
},
{
siteld = 56,
id = 326,
fid = 78,
node = "保定",
},
{
siteld = 5,
id = 13,
fid = 2,
node = "北京",
},
{
siteld = 63,
id = 114,
fid = 85,
node = "长春",
}
}
//其中_indexs这个数组里面装索引,_dataArray这个里面装实体数据模型,遇到多音字的话,统一转化成拼音进行排序
- (void)sortArray
{
_indexs = [NSMutableArray array];
_dataArrayTmp=[[NSMutableArray alloc] init];
_dataArrayTmp = (NSMutableArray *)_modelArray;//将数据传给临时数组,对临时数组进行排序
//*************增加排序和索引**************8*
_dataArray=[NSMutableArray arrayWithCapacity:1];
_indexedCollation=[UILocalizedIndexedCollation currentCollation];
for (YFGetCItysModel *model in _dataArrayTmp) {
NSInteger section=[_indexedCollation sectionForObject:model collationStringSelector:@selector(areaNamePinYinStr)];
//这里的addressModel是一个类,里面有name属性,这个方法是按照name来分区。
model.section=section;
}
NSInteger sectionCount=[[_indexedCollation sectionTitles] count];
[self assemblyData:sectionCount];
}
-(void)assemblyData:(NSInteger )sectionCount{
NSMutableArray *sectionsArray=[NSMutableArray arrayWithCapacity:sectionCount];//这里使用的是二维数组,这是外面一层
for (int i=0; i<=sectionCount; ++i) {
NSMutableArray *singleSectionArray=[NSMutableArray arrayWithCapacity:1];//这是在每个外层数组里面初始化子数组
[sectionsArray addObject:singleSectionArray];
}
for(YFGetCItysModel *model in _dataArrayTmp){//将对象放到正确的区段当中
[(NSMutableArray *) [sectionsArray objectAtIndex:model.section] addObject:model];
}
for (NSMutableArray *singleSectionArray in sectionsArray){//排序子数组中的数据
NSArray *sortedSection=[_indexedCollation sortedArrayFromArray:singleSectionArray collationStringSelector:@selector(areaNamePinYinStr)];
[_dataArray addObject:sortedSection];
if (singleSectionArray.count > 0) {
YFGetCItysModel *model = singleSectionArray[0];
char c = 'A'; c += model.section;
if (![_indexs containsObject:[NSString stringWithFormat:@"%c",c]]) {
[_indexs addObject:[NSString stringWithFormat:@"%c",c]];
}
}
}
NSMutableArray *lastArray = [NSMutableArray arrayWithArray:_dataArray];
for (NSMutableArray *array in lastArray) {
if (array.count == 0) {
[_dataArray removeObject:array];
}
}
}