注:本文是根据数据模型WECompanyModel的某个属性firstLetter进行排序
一、首先设置一个索引属性
@property (nonatomic, strong) UILocalizedIndexedCollation *collation;
二、设置右侧字母索引的背景和颜色
self.tableView.sectionIndexColor = RGBAlphaColorValue(0x999999);
self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
self.tableView.sectionIndexTrackingBackgroundColor = RGBAlphaColorValue(0xf3f3f3);
三、设置分组字母索引的数据源和代理
// 设置section的Header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *userObjsInSection = [_dataArray objectAtIndex:section];
if(userObjsInSection == nil || [userObjsInSection count] <= 0) {
return nil;
}
return [[_collation sectionTitles] objectAtIndex:section];
}
// 设置索引标题
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [_collation sectionIndexTitles];
}
// 关联搜索
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [_collation sectionForSectionIndexTitleAtIndex:index];
}
四、网络请求列表,将列表数据数组内容进行排序
#pragma mark - Request
- (void)requestDatas {
NSString *path = [NSString stringWithFormat:@"label/cfcp/%@",@(self.pid)];
[WEHttpRequest requestWithConfig:^(WEUrlRequestConfig *request) {
request.baseUrl = HostUrl;
request.url = path;
request.methodType = WEHttpMethodGET;
request.timeoutInterval = 15;
request.parameters = self.params;
[request setValue:[WELoginManager sharedInstance].accessToken forHeaderField:kAccessToken];
} success:^(NSDictionary *responseObject) {
[WEHUD hideHUD];
NSArray *dataArray = [NSArray array];
dataArray = responseObject[@"data"];
[self.dataArray removeAllObjects];
if (dataArray.count > 0) {
NSArray *tempArray = [NSArray modelArrayWithClass:WECompanyModel.class json:dataArray];
//获得当前UILocalizedIndexedCollation对象并且引用赋给collation,A-Z的数据
self.collation = [UILocalizedIndexedCollation currentCollation];
//获得索引数和section标题数
NSInteger index, sectionTitlesCount = [[_collation sectionTitles] count];
//临时数据,存放section对应的userObjs数组数据
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
//设置sections数组初始化:元素包含userObjs数据的空数据
for (index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[newSectionsArray addObject:array];
}
//将用户数据进行分类,存储到对应的sesion数组中
for (WECompanyModel *model in tempArray) {
//根据timezone的localename,获得对应的的section number
NSInteger sectionNumber = [_collation sectionForObject:model collationStringSelector:@selector(firstLetter)];// 根据WECompanyModel模型的字段属性firstLetter进行排序,也可根据name属性进行排序,排序可能花费时间较长
//获得section的数组
NSMutableArray *sectionUserObjs = [newSectionsArray objectAtIndex:sectionNumber];
//添加内容到section中
[sectionUserObjs addObject:model];
}
//排序,对每个已经分类的数组中的数据进行排序,如果仅仅只是分类的话可以不用这步
for (index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *userObjsArrayForSection = [newSectionsArray objectAtIndex:index];
//获得排序结果,根据模型的firstLetter字段进行排序
NSArray *sortedUserObjsArrayForSection = [_collation sortedArrayFromArray:userObjsArrayForSection collationStringSelector:@selector(firstLetter)];
//替换原来数组
[newSectionsArray replaceObjectAtIndex:index withObject:sortedUserObjsArrayForSection];
}
self.dataArray = newSectionsArray;
[self.tableView reloadData];
[self.tableView.mj_header endRefreshing];
} else {
[self.dataArray removeAllObjects];
[self.tableView reloadData];
[self.tableView.mj_header endRefreshing];
}
} failed:^(NSError *error) {
debugMethod();
}];
}
网友评论