IOS开发中实现UITableView按照首字母将集合进行检索分组
2015-08-12 14:22
501 查看
在开发公司项目中遇到了将图书目录进行按照首字母分组排序的问题
1.在项目添加解析汉字拼音的Pinyin.h文件
View Code
3.处理服务器返回的数据
4.实现UITabView的一些必要的代理方法
IOS开发技术交流QQ群:491355147 欢迎加入一起讨论技术哦
1.在项目添加解析汉字拼音的Pinyin.h文件
// // ChineseString.m // // Created by leo on 15/8/11. // Copyright (c) 2015年 fo. All rights reserved. // #import "ChineseString.h" @implementation ChineseString @synthesize string; @synthesize pinYin; #pragma mark - 返回tableview右方 indexArray +(NSMutableArray*)IndexArray:(NSArray*)stringArr { NSMutableArray *tempArray = [self ReturnSortChineseArrar:stringArr]; NSMutableArray *A_Result=[NSMutableArray array]; NSString *tempString ; for (NSString* object in tempArray) { NSString *pinyin = [((ChineseString*)object).pinYin substringToIndex:1]; //不同 if(![tempString isEqualToString:pinyin]) { [A_Result addObject:pinyin]; tempString = pinyin; } } return A_Result; } #pragma mark - +(NSMutableArray*)LetterSortArray:(NSArray*)stringArr { NSMutableArray *tempArray = [self ReturnSortChineseArrar:stringArr]; NSMutableArray *LetterResult=[NSMutableArray array]; NSMutableArray *item = [NSMutableArray array]; NSString *tempString ; //拼音分组 for (NSObject* object in tempArray) { NSString *pinyin = [((ChineseString*)object).pinYin substringToIndex:1]; PictureBook *book=((ChineseString*)object).book; //不同 if(![tempString isEqualToString:pinyin]) { //分组 item = [NSMutableArray array]; [item addObject:book]; [LetterResult addObject:item]; //遍历 tempString = pinyin; }else//相同 { [item addObject:book]; } } return LetterResult; } /** * 过滤指定字符串 里面的指定字符根据自己的需要添加 */ +(NSString*)RemoveSpecialCharacter: (NSString *)str { NSRange urgentRange = [str rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString: @",.?、 ~¥#&<>《》()[]{}【】^@/£¤|§¨「」『』¢¬ ̄~@#&*()——+|《》$_€"]]; if (urgentRange.location != NSNotFound) { return [self RemoveSpecialCharacter:[str stringByReplacingCharactersInRange:urgentRange withString:@""]]; } return str; } /** * 返回排序好的字符拼音 * */ +(NSMutableArray*)ReturnSortChineseArrar:(NSArray*)stringArr { //获取字符串中文字的拼音首字母并与字符串共同存放 NSMutableArray *chineseStringsArray=[NSMutableArray array]; for(int i=0;i<[stringArr count];i++) { ChineseString *chineseString=[[ChineseString alloc]init]; PictureBook *book=[stringArr objectAtIndex:i]; chineseString.book=book; chineseString.string=[NSString stringWithString:book.title]; if(chineseString.string==nil){ chineseString.string=@""; } //去除两端空格和回车 chineseString.string = [chineseString.string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //此方法存在一些问题 有些字符过滤不了 //NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-*+=_\\|~<>$€^•'@#$%^&*()_+'\""]; //chineseString.string = [chineseString.string stringByTrimmingCharactersInSet:set]; //这里我自己写了一个递归过滤指定字符串 RemoveSpecialCharacter chineseString.string =[ChineseString RemoveSpecialCharacter:chineseString.string]; // NSLog(@"string====%@",chineseString.string); //判断首字符是否为字母 NSString *regex = @"[A-Za-z]+"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; NSString *initialStr = [chineseString.string length]?[chineseString.string substringToIndex:1]:@""; if ([predicate evaluateWithObject:initialStr]) { NSLog(@"chineseString.string== %@",chineseString.string); //首字母大写 chineseString.pinYin = [chineseString.string capitalizedString] ; }else{ if(![chineseString.string isEqualToString:@""]){ NSString *pinYinResult=[NSString string]; for(int j=0;j<chineseString.string.length;j++){ NSString *singlePinyinLetter=[[NSString stringWithFormat:@"%c", pinyinFirstLetter([chineseString.string characterAtIndex:j])]uppercaseString]; pinYinResult=[pinYinResult stringByAppendingString:singlePinyinLetter]; } chineseString.pinYin=pinYinResult; }else{ chineseString.pinYin=@""; } } [chineseStringsArray addObject:chineseString]; } //按照拼音首字母对这些Strings进行排序 NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"pinYin" ascending:YES]]; [chineseStringsArray sortUsingDescriptors:sortDescriptors]; return chineseStringsArray; } #pragma mark - 返回一组字母排序数组 +(NSMutableArray*)SortArray:(NSArray*)stringArr { NSMutableArray *tempArray = [self ReturnSortChineseArrar:stringArr]; //把排序好的内容从ChineseString类中提取出来 NSMutableArray *result=[NSMutableArray array]; for(int i=0;i<[stringArr count];i++){ [result addObject:((ChineseString*)[tempArray objectAtIndex:i]).string]; } return result; } @end
View Code
3.处理服务器返回的数据
//定义数据 @property(nonatomic,strong)NSMutableArray *indexArray;//去重后的首字母 @property(nonatomic,strong)NSMutableArray *letterResultArr;//按照首字母排序后的集合 if (ret) { self.indexArray = [ChineseString IndexArray:ret]; self.letterResultArr = [ChineseString LetterSortArray:ret]; [self.tableView reloadData]; }
4.实现UITabView的一些必要的代理方法
#pragma mark -Table View Data Source Methods #pragma mark -设置右方表格的索引数组 -(NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView{ return self.indexArray; } -(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{ return index; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [self.indexArray count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[self.LetterResultArr objectAtIndex:section]count]; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 80; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ PictureBook *book=[[self.LetterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]; BookDetailViewController *vc = [[BookDetailViewController alloc]init]; vc.title=book.title; vc.book=book; [self.navigationController pushViewController:vc animated:YES]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { BookItemViewCell *cell = [BookItemViewCell cellWithTableView:tableView]; cell.book=[[self.LetterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]; cell.delegate=self; return cell; }
IOS开发技术交流QQ群:491355147 欢迎加入一起讨论技术哦
相关文章推荐
- 小技巧之巧利用identified by values重置用户密码
- 使用XIB拖出来的UIButton,selected状态下,button出现多余背景颜色
- iOS UI10_cell自适应高度
- LoadRunner录制脚本,报连接不到服务器的提示信息 :Request Connection: Remote Serve
- H264 primer - A layman’s guide - by KUSH AMERASINGHE
- iOS UI10_tableView的编辑
- iOS UI10_带分区的省市区
- Diesel engine genuinely is aware of which a excellent women enjoy
- UITableView取消选中的cell方法
- UE4 Particle System Reference
- UIButton 的图片自动拉伸
- IOS UIVIEW layer动画 总结
- [Erlang_Question21]Erlang性能分析工具eprof fporf的应用
- HDU 3397 Sequence operation
- volley建立请求队列(Setting Up a RequestQueue)
- 35.标签栏(UITabBarController)
- STL中vector、list、deque的区别
- Ios 中的UI自动化测试
- iOS UIImage 转NSString
- Break 与 Continue的区别