使用UILocalizedIndexedCollation实现区域索引排序 及 不显示没有数据的区域
2016-05-27 14:39
387 查看
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--;//这里很重要
}
}
相关文章推荐
- window.event.returnValue
- 05.手把手教你 .Net EasyUI DataGrid(完整的增删查改)
- 建议SQLite操作使用rawQuery方法
- Android UI性能优化实战 识别绘制中的性能问题
- Win10无法安装提示磁盘布局不受UEFI固件支持怎样解决
- iOS-如何通过UIEdgeInsetsMake来制作可伸缩的Button
- String,StringBuffer,StringBuilder的区别
- UITableview
- UIButton实现左文字右图片
- UIImage自适应UIImageView的frame大小
- vs2010中的MSBuild输出warning MSB8012问题
- nova rebuild
- Anti-prime Sequences
- 你真的了解Instant Run吗?
- D与C#的GUI内存占用比较
- 【EasyUI+MVC-搭建后台框架】
- 瀑布流+UICollectionView
- call by value 和 call by reference
- CEFGlue 加载FLASH 使用HOOK 方式 解决 CEF 加载 PPAPI FLASH 插件时弹出 CMD 命令行 窗口的问题
- easyui datagrid自定义按钮列,即最后面的操作列