为了实现上图字母分类排序
实现过程借鉴了网上 大神的博客笔记
http://www.cnblogs.com/fxiaoquan/p/4724208.html
实现过程如下:
下载 pinyin.c 和 pinyin.h 文件 导入到项目中
这里创建一个处理对象的数据模型
将大神的代码copy到模型中 替换成自己想要的数据模型
//
// ChineseString.h
// SAGA_iOS
//
// Created by location on 16/8/10.
// Copyright © 2016年 it.sozi. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "PinYin.h"
#import "CZHomeSearchBrandItem.h"
@interface ChineseString : NSObject
@property(nonatomic,copy) NSString *string;
@property(nonatomic,copy) NSString *pinYin;
@property(nonatomic,strong)CZHomeSearchBrandItem *brandItem;
/**
* TableView右方IndexArray
*/
+(NSMutableArray *) IndexArray:(NSArray *) stringArr;
/**
* 文本列表
*/
+(NSMutableArray *) LetterSortArray:(NSArray *)stringArr;
/**
*返回一组字母排列数组(中英混排)
*/
+(NSMutableArray *) SortArray:(NSArray *)stringArr;
实现文件
//
// ChineseString.m
// SAGA_iOS
//
// Created by location on 16/8/10.
// Copyright © 2016年 it.sozi. All rights reserved.
//
#import "ChineseString.h"
@implementation ChineseString
- ( id )init {
if ( self = [ super init ]) {
}
return self ;
}
#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];
CZHomeSearchBrandItem *brandItem=((ChineseString*)object).brandItem;
//不同
if(![tempString isEqualToString:pinyin])
{
//分组
item = [NSMutableArray array];
[item addObject:brandItem];
[LetterResult addObject:item];
//遍历
tempString = pinyin;
}else//相同
{
[item addObject:brandItem];
}
}
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];
CZHomeSearchBrandItem *brandItem=[stringArr objectAtIndex:i];
chineseString.brandItem=brandItem;
chineseString.string=[NSString stringWithString:brandItem.name];
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;
}
在获取数据的控制器页面导入#import “ChineseString.h”
@property(nonatomic,strong)NSMutableArray *indexArray;//去重后的首字母
@property(nonatomic,strong)NSMutableArray *letterResultArr;//按照首字母排序后的集合
初始化两个数组来接收排序后的数据
实现相应的类方法
-(void)setWatchBrands:(NSArray *)watchBrands
{
_watchBrands = watchBrands;
if (watchBrands) {
//返回tableview右方 indexArray
self.indexArray = [ChineseString IndexArray:watchBrands];
self.letterResultArr = [ChineseString LetterSortArray:watchBrands];
}
[self.tableView reloadData];
}
以上就已经将后台返回的数据 进行字母编排 编排后赋值给相应的数组 tableView 实现对应的数据源 和代理方法
//多少组
- (NSInteger)numberOfSectionsInTableView:( UITableView *)tableView
{
return self.indexArray.count;
}
//每组中有多少行
- (NSInteger)tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section
{
return [[self.letterResultArr objectAtIndex:section]count];
}
// 为 section 添加标题
- (NSString *)tableView:( UITableView *)tableView titleForHeaderInSection:( NSInteger )section
{
return [self.indexArray objectAtIndex :section];
}
//tableView 右边显示的字母
- (NSArray *)sectionIndexTitlesForTableView:( UITableView *)tableView
{
return self.indexArray;
}
#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *brandID =@"brandID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:brandID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:brandID];
}
CZHomeSearchBrandItem *brandItem = [[self.letterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
cell.textLabel.text = brandItem.name;
cell.textLabel.font = CZFontMiddle;
cell.textLabel.textColor = CZColor(@"#333333");
return cell;
}
展示效果如上图
非常感谢大神的代码 让我实现了字母分类分组排序
详细实现可参照 此链接 http://www.cnblogs.com/fxiaoquan/p/4724208.html