您的位置:首页 > 产品设计 > UI/UE

使用UILocalizedIndexedCollation实现区域索引排序 及 不显示没有数据的区域

2016-05-27 14:39 387 查看
UILocalizedIndexedCollation可以实现区域排序,类似通讯录的样式。 //首先进行初始化
locationCollation=[UILocalizedIndexedCollationcurrentCollation];

//获取section标题A-Z#

array_collation_title=[[NSMutableArrayalloc]initWithArray:[locationCollationsectionTitles]];

//构建每个section数组
array_section=[[NSMutableArrayalloc]init];
for(inti=0;i<array_collation_title.count;i++){
NSMutableArray*subArray=[[NSMutableArrayalloc]init];
[array_sectionaddObject:subArray];
}
//排序
//排序对象,放进分区数组中
for(Person*personinarray_datas){
NSIntegersection=[locationCollationsectionForObject:personcollationStringSelector:@selector(name)];
NSMutableArray*subarray=array_section[section];
[subarrayaddObject:person];
}

//分别对分区进行排序
for(inti=0;i<array_section.count;i++){
NSMutableArray*subarray=[array_sectionobjectAtIndex:i];
NSArray*sortArr=[locationCollationsortedArrayFromArray:subarraycollationStringSelector:@selector(name)];
[array_sectionreplaceObjectAtIndex:iwithObject:sortArr];
}

  


#pragmamark-datasouth
//返回section要显示的标题集合
-(NSArray<NSString*>*)sectionIndexTitlesForTableView:(UITableView*)tableView{
//return[[UILocalizedIndexedCollationcurrentCollation]sectionTitles];
returnarray_collation_title;
}

//根据section返回显示的标题
-(NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section{
//return[[[UILocalizedIndexedCollationcurrentCollation]sectionTitles]objectAtIndex:section];
return[array_collation_titleobjectAtIndex:section];
}

//点击右侧字母,触发此方法,告诉数据源选中的section
-(NSInteger)tableView:(UITableView*)tableViewsectionForSectionIndexTitle:(NSString*)titleatIndex:(NSInteger)index{
return[[UILocalizedIndexedCollationcurrentCollation]sectionForSectionIndexTitleAtIndex:index];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
intcount=0;
for(NSMutableArray*subArrinarray_section){
if(subArr.count>0){
count++;
}
}
returncount;
}
-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{
NSMutableArray*subArr=[array_sectionobjectAtIndex:section];
returnsubArr.count;
}

-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:@"Cell"];
if(cell==nil){
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"Cell"];
}
NSMutableArray*subArray=[array_sectionobjectAtIndex:indexPath.section];
Person*person=[subArrayobjectAtIndex:indexPath.row];
if(person!=nil){
[cell.textLabelsetText:person.name];
}

returncell;
}

-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{
NSLog(@"section=%d,row=%d",indexPath.section,indexPath.row);
}

-(CGFloat)tableView:(UITableView*)tableViewheightForFooterInSection:(NSInteger)section{
return15;
}
-(CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section{
return15;
}






运行效果图



运行后发现在没有值的section下依然显示section标题,这样会感觉非常丑。

优化一下代码,

添加以下方法

//删除没有值的subArr
for(inti=0;i<array_section.count;i++){
NSMutableArray*subarray=[array_sectionobjectAtIndex:i];
if(subarray.count==0){
[array_sectionremoveObjectAtIndex:i];
[array_collation_titleremoveObjectAtIndex:i];
i--;//这里很重要
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: