1,uitableview中涉及到排序的问题,查找资料后发现使用UILocalizedIndexedCollation可以很好处理中文和英文系统下中文的排序。而且如果第一个汉字首字母一样那么就会按照第二个开始排序。

2,如果tableview的高度不是整个全屏的,比如有导航栏定制等,那么ios7.0下会缩减显示。呈现的是带有圆点的效果。其他系统不会。且无法修改。

效果如下:

代码如下:

#pragma mark -
#pragma mark Table view data source and delegate methods
//设置Section的数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // The number of sections is the same as the number of titles in the collation.
    return [[collation sectionTitles] count];
}

//设置每个Section下面的cell数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    // The number of time zones in the section is the count of the array associated with the section in the sections array.
    NSArray *friendsInSection = [sectionsArray objectAtIndex:section];
    
    return [friendsInSection count];
}

//设置每行的cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.customTable = tableView;
    static NSString *CellIdentifier = @"Friends";
    
    UITableViewCell *cell = [self.customTable dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;

}
    
    // Get the time zone from the array associated with the section index in the sections array.
    NSArray *friendsInSection = [sectionsArray objectAtIndex:indexPath.section];
    
    // Configure the cell with the time zone's name.
    FriendsWrapper *friend = [friendsInSection objectAtIndex:indexPath.row];
    cell.textLabel.text = friend.localeName;
    
    return cell;
}

/*
 Section-related methods: Retrieve the section titles and section index titles from the collation.
 */

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell*cell =[self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    return cell.frame.size.height;
}

//设置section的Header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    //NSLog(@"%d",[[self.sectionsArray objectAtIndex:section]count]);
        if ([[self.sectionsArray objectAtIndex:section]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];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -
#pragma mark Set the data array and configure the section data

- (void)setFriendsListArray:(NSMutableArray *)newDataArray {
    if (newDataArray != friendsListArray) {
        [friendsListArray release];
        friendsListArray = [newDataArray retain];
    }
    if (friendsListArray == nil) {
        self.sectionsArray = nil;
    }
    else {
        [self configureSections];
    }
}

- (void)configureSections {
    
    //获得当前UILocalizedIndexedCollation对象并且引用赋给collation
    self.collation = [UILocalizedIndexedCollation currentCollation];
    //获得索引数和section标题数
    NSInteger index, sectionTitlesCount = [[collation sectionTitles] count];
    
    NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
    
    //设置sections数组:元素包含timezone
    for (index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [newSectionsArray addObject:array];
        [array release];
    }
    
    // Segregate the time zones into the appropriate arrays.
    for (FriendsWrapper *friend in friendsListArray) {
        
        //根据timezone的localename,获得对应的时区的section number
        NSInteger sectionNumber = [collation sectionForObject:friend collationStringSelector:@selector(localeName)];
        
        //获得section的数组
        NSMutableArray *sectionFriends = [newSectionsArray objectAtIndex:sectionNumber];
        
        //添加时区内容到section中 sectionTimeZones 存放的是所有的字符串
        [sectionFriends addObject:friend];
    }
    
    //排序
    for (index = 0; index < sectionTitlesCount; index++)
   
    {
        
        NSMutableArray *friendsListArrayForSection = [newSectionsArray objectAtIndex:index];
        
        //获得排序结果
        NSArray *sortedFriendsArrayForSection = [collation sortedArrayFromArray:friendsListArrayForSection collationStringSelector:@selector(localeName)];
        
        //替换原来数组
        [newSectionsArray replaceObjectAtIndex:index withObject:sortedFriendsArrayForSection];
    }
    
    self.sectionsArray = newSectionsArray;
    [newSectionsArray release];
}

#import "FriendsWrapper.h"

@implementation FriendsWrapper

@synthesize localeName;

- (id)initWithFriends:(NSString *)nameComponents
{
    
    if (self = [super init]) {
        
        NSString *name = nil;
        
        if ((nameComponents == nil) ||
            (nameComponents.length == 0)){
            
            name = @"";
        }else
        {
            name = nameComponents;
        }
        /*if ([nameComponents count] == 2) {
         name = [nameComponents objectAtIndex:1];
         }
         if ([nameComponents count] == 3) {
         name = [NSString stringWithFormat:@"%@ (%@)", [nameComponents objectAtIndex:2], [nameComponents objectAtIndex:1]];
         }*/
        
        localeName = [[name stringByReplacingOccurrencesOfString:@"_" withString:@" "] retain];
    }
    return self;
}

- (void)dealloc {
    [localeName release];    
    [super dealloc];
}

@end

uitableview中文排序问题的更多相关文章

  1. C++ sqlite3解决中文排序问题

    导言:sqlite3默认的编码方式为UTF8编码,而在UTF8编码下,中文不是按照拼音顺序编码的,所以想解决中文排序问题,必须自定义排序规则,将UTF8编码转换成GB2312编码(GB2312编码中文 ...

  2. Jquery datatable中文排序问题

    先扩展datatable的的排序功能,添加一个自定义排序函数 //为jq datatable 自定义中文排序 jQuery.fn.dataTableExt.oSort['chinese-sort-as ...

  3. Oracle的order by的中文排序问题

    Oracle 中查询结果按照某个中文字段或者英文字母(包括 符号)排序,并不会得到我们预期的结果,因为对于中文与英文字母及符号,Oracle实际是按照其对应的ASCII码值排序的! 可以看到按照中文村 ...

  4. Oracle中文排序问题

    默认感觉中文是按拼音排序,如果没实现效果,请加上其它排序,例如日期 表名为 dept ,其中name字段是中文,下面分别实现按照单位名称的笔划.部首和拼音排序.1: //按照笔划排序2: select ...

  5. oracle的中文排序问题

    mysql中文排序有convert(name using gbk)这样的函数,于是研究了一下oracle中文排序: 使用拼音排序 SQL> select * from chineseordert ...

  6. mysql中文排序问题

    mysql中文排序,用到的是: SELECT id id, billId billId, namespec nameSpec, unit unit, amount amount, price pric ...

  7. DB2 中文排序问题

    本地测试库中 代码集: GBK 数据库配置发行版级别 = 0x0c00 数据库发行版级别 = 0x0c00 数据库地域 = CN 数据库代码页 = 1386 数据库代码集 = GBK 数据库国家/地区 ...

  8. ExtJS4.2学习(四)Grid表格中文排序问题(转)

    鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-07/173.html --------------- ...

  9. java中文排序问题(转)

    在Java中,对一个数组或列表(在本文中统称为集合)中的元素排序,是一个很经常的事情.好在Sun公司在Java库中实现了大部分功能.如果集合中的元素实现了Comparable接口,调用以下的静态(st ...

随机推荐

  1. 重新造轮子之静态链接1(Static linking)

    最近学习计算机病毒学的过程中,又讲到了静态链接的问题,联想到了之前保健哥在信息安全的课堂上向我们展示了一个没有main()函数的C程序到底应该如何编写.个人觉得这个小实验对于加深静态链接的过程的理解也 ...

  2. 《Scrum实战》第0次课【如何学习敏捷】全团课后任务汇总

    <Scrum实战>第0次课作业 完成情况: 课程名称:如何学习敏捷 1组 孟帅 孟帅: http://www.cnblogs.com/mengshuai1982/p/7096338.htm ...

  3. day05 模块以及内置常用模块用法

    内置常用模块详解: 1 time 2 datetime 3 random   4 os 5 sys 6 shutil 7 shelve 8 xml 9 configparser 10 hashlib ...

  4. 使用 Scene 类在 XNA 中创建不同的场景(八)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  5. netcore命令行部署|跨域问题

    1.在hosting中修改发布端口号,如遇见不识别IP则改成*再用命令行运行 { "server.url": "http://*:8089"} 3.给接口开外网 ...

  6. poj1985&&第四次CCF软件认证第4题 求树的直径

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4216   Accepted: 2137 Case ...

  7. 九度oj 题目1084:整数拆分 清华大学2010年机试题目

    题目描述: 一个整数总可以拆分为2的幂的和,例如:7=1+2+4 7=1+2+2+2 7=1+1+1+4 7=1+1+1+2+2 7=1+1+1+1+1+2 7=1+1+1+1+1+1+1总共有六种不 ...

  8. 九度oj 题目1392:排序生成最小的数

    题目描述: 还记得陈博是个数字完美主义者么?^_^....这次,他又闹脾气了!我们知道计算机中常常要使用数组保存一组数字,但是今天他就要求把数组里的所有数字组成一个,并且这个数字是这些数字所能组成的所 ...

  9. 高并发下的HashMap,ConcurrentHashMap

    参照: http://mp.weixin.qq.com/s/dzNq50zBQ4iDrOAhM4a70A http://mp.weixin.qq.com/s/1yWSfdz0j-PprGkDgOomh ...

  10. 第002弹:Java 中的值传递和引用传递

    在 Java 的代码开发过程中,为了尽可能提高方法的复用性,明确方法的作用,同时防止一个方法内部过于臃肿的问题,往往会创建许多方法,那么不可避免地会涉及到参数传递的问题.通常来说,我们将 Java 中 ...