uitableview中文排序问题
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中文排序问题的更多相关文章
- C++ sqlite3解决中文排序问题
导言:sqlite3默认的编码方式为UTF8编码,而在UTF8编码下,中文不是按照拼音顺序编码的,所以想解决中文排序问题,必须自定义排序规则,将UTF8编码转换成GB2312编码(GB2312编码中文 ...
- Jquery datatable中文排序问题
先扩展datatable的的排序功能,添加一个自定义排序函数 //为jq datatable 自定义中文排序 jQuery.fn.dataTableExt.oSort['chinese-sort-as ...
- Oracle的order by的中文排序问题
Oracle 中查询结果按照某个中文字段或者英文字母(包括 符号)排序,并不会得到我们预期的结果,因为对于中文与英文字母及符号,Oracle实际是按照其对应的ASCII码值排序的! 可以看到按照中文村 ...
- Oracle中文排序问题
默认感觉中文是按拼音排序,如果没实现效果,请加上其它排序,例如日期 表名为 dept ,其中name字段是中文,下面分别实现按照单位名称的笔划.部首和拼音排序.1: //按照笔划排序2: select ...
- oracle的中文排序问题
mysql中文排序有convert(name using gbk)这样的函数,于是研究了一下oracle中文排序: 使用拼音排序 SQL> select * from chineseordert ...
- mysql中文排序问题
mysql中文排序,用到的是: SELECT id id, billId billId, namespec nameSpec, unit unit, amount amount, price pric ...
- DB2 中文排序问题
本地测试库中 代码集: GBK 数据库配置发行版级别 = 0x0c00 数据库发行版级别 = 0x0c00 数据库地域 = CN 数据库代码页 = 1386 数据库代码集 = GBK 数据库国家/地区 ...
- ExtJS4.2学习(四)Grid表格中文排序问题(转)
鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-07/173.html --------------- ...
- java中文排序问题(转)
在Java中,对一个数组或列表(在本文中统称为集合)中的元素排序,是一个很经常的事情.好在Sun公司在Java库中实现了大部分功能.如果集合中的元素实现了Comparable接口,调用以下的静态(st ...
随机推荐
- Solution: 最近公共祖先·一 [hiho一下 第十三周]
题目1 : 最近公共祖先·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho最近发现了一个神奇的网站!虽然还不够像58同城那样神奇,但这个网站仍然让小Ho乐在其中 ...
- HBase0.94.2-cdh4.2.0需求评估测试报告1.0之一
hbase是bigtable的开源山寨版本.是建立的hdfs之上,提供高可靠性.高性能.列存储.可伸缩.实时读写的数据库系统.它介于nosql和RDBMS之间,仅能通过主键(row key)和主键的r ...
- JVM执行子系统探究——类文件结构初窥
类文件(.class)是搞java的都非常熟悉的文件,一般我们在编写java之后文件之后,首先通过javac工具生成.class类字节码文件,而后在执行程序的时候由虚拟机加载执行.那么为什么要生成.c ...
- ssl 在nginx上的部署示例
server { listen 80; listen 443 ssl; server_name [DOMAIN]; ssl on; ssl_certificate /work/ss ...
- Java项目引入eclipse注意事项
我以前也搞过java,后转前端,接触html+css+js时间比较多,所以java后端也忘了差不多.最近负责公司的邮件系统项目,项目是java语言写,项目架构比较复杂,在部署项目的时候,遇到了很多问题 ...
- 理解依赖注入 for Zend framework 2
依赖注入(Dependency Injection),也成为控制反转(Inversion of Control),一种设计模式,其目的是解除类之间的依赖关系. 假设我们需要举办一个Party,Part ...
- sql中group by 和having 用法解析
--sql中的group by 用法解析:-- Group By语句从英文的字面意义上理解就是“根据(by)一定的规则进行分组(Group)”.--它的作用是通过一定的规则将一个数据集划分成若干个小的 ...
- Python面试题(练习二)
1.用Python实现一个二分查找的函数. data = [1, 3, 6, 7, 9, 12, 14, 16, 17, 18, 20, 21, 22, 23, 30, 32, 33, 35] def ...
- 【bzoj1283】序列 线性规划与费用流
题目描述 给出一个长度为 的正整数序列Ci,求一个子序列,使得原序列中任意长度为 的子串中被选出的元素不超过K(K,M<=100) 个,并且选出的元素之和最大. 输入 第1行三个数N,m,k. ...
- iOS-汉字排序
* 在IOS开发过程中,排序是我们经常遇到的问题,那么如何进行排序呢? * 在英文状态下,系统中有直接可以调用的方法. 例如:对数组[sss, aaa, bbb, ppp]进行排序,我们可以直接 ...