UILocalizedIndexedCollation
来源:互联网 发布:网络嗅探1.6 编辑:程序博客网 时间:2024/04/30 16:47
#pragma mark -------配置分组信息------
#define NEW_USER(str) [[User alloc] init:str name:str]
//配置分组信息
- (
void
)configureSections {
//初始化测试数据
userArray = [[
NSMutableArray
alloc] init];
[userArray addObject:NEW_USER(@
"test001"
)];
[userArray addObject:NEW_USER(@
"test002"
)];
[userArray addObject:NEW_USER(@
"test003"
)];
[userArray addObject:NEW_USER(@
"test004"
)];
[userArray addObject:NEW_USER(@
"test005"
)];
[userArray addObject:NEW_USER(@
"adam01"
)];
[userArray addObject:NEW_USER(@
"adam02"
)];
[userArray addObject:NEW_USER(@
"adam03"
)];
[userArray addObject:NEW_USER(@
"bobm01"
)];
[userArray addObject:NEW_USER(@
"bobm02"
)];
[userArray addObject:NEW_USER(@
"what01"
)];
[userArray addObject:NEW_USER(@
"0what02"
)];
[userArray addObject:NEW_USER(@
"李一"
)];
[userArray addObject:NEW_USER(@
"李二"
)];
[userArray addObject:NEW_USER(@
"胡一"
)];
[userArray addObject:NEW_USER(@
"胡二"
)];
//获得当前UILocalizedIndexedCollation对象并且引用赋给collation,A-Z的数据
self
.collation = [
UILocalizedIndexedCollation
currentCollation];
//获得索引数和section标题数
NSInteger
index, sectionTitlesCount = [[collation sectionTitles] count];
//临时数据,存放section对应的userObjs数组数据
NSMutableArray
*newSectionsArray = [[
NSMutableArray
alloc] initWithCapacity:sectionTitlesCount];
//设置sections数组初始化:元素包含userObjs数据的空数据
for
(index = 0; index < sectionTitlesCount; index++) {
NSMutableArray
*array = [[
NSMutableArray
alloc] init];
[newSectionsArray addObject:array];
}
//将用户数据进行分类,存储到对应的sesion数组中
for
(User *userObj in userArray) {
//根据timezone的localename,获得对应的的section number
NSInteger
sectionNumber = [collation sectionForObject:userObj collationStringSelector:
@selector
(username)];
//获得section的数组
NSMutableArray
*sectionUserObjs = [newSectionsArray objectAtIndex:sectionNumber];
//添加内容到section中
[sectionUserObjs addObject:userObj];
}
//排序,对每个已经分类的数组中的数据进行排序,如果仅仅只是分类的话可以不用这步
for
(index = 0; index < sectionTitlesCount; index++) {
NSMutableArray
*userObjsArrayForSection = [newSectionsArray objectAtIndex:index];
//获得排序结果
NSArray
*sortedUserObjsArrayForSection = [collation sortedArrayFromArray:userObjsArrayForSection collationStringSelector:
@selector
(username)];
//替换原来数组
[newSectionsArray replaceObjectAtIndex:index withObject:sortedUserObjsArrayForSection];
}
self
.sectionsArray = newSectionsArray;
}
#pragma mark --------tableview的委托和datasource-------
//设置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
*UserObjsInSection = [sectionsArray objectAtIndex:section];
return
[UserObjsInSection count];
}
//设置每行的cell的内容
- (
UITableViewCell
*)tableView:(
UITableView
*)tableView cellForRowAtIndexPath:(
NSIndexPath
*)indexPath {
static
NSString
*CellIdentifier = @
"Cell"
;
UITableViewCell
*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if
(cell ==
nil
) {
cell = [[
UITableViewCell
alloc] initWithStyle:
UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Get the time zone from the array associated with the section index in the sections array.
NSArray
*userNameInSection = [sectionsArray objectAtIndex:indexPath.section];
// Configure the cell with the time zone's name.
User *userObj = [userNameInSection objectAtIndex:indexPath.row];
cell.textLabel.text = userObj.username;
return
cell;
}
- (
void
)tableView:(
UITableView
*)tableView didSelectRowAtIndexPath:(
NSIndexPath
*)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:
YES
];
}
/*
* 跟section有关的设定
*/
//设置section的Header
- (
NSString
*)tableView:(
UITableView
*)tableView titleForHeaderInSection:(
NSInteger
)section {
NSArray
*UserObjsInSection = [sectionsArray objectAtIndex:section];
if
(UserObjsInSection ==
nil
|| [UserObjsInSection 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];
}
0 0
- UILocalizedIndexedCollation
- UILocalizedIndexedCollation 使用
- 13.UILocalizedIndexedCollation
- UILocalizedIndexedCollation简介
- iOS UILocalizedIndexedCollation
- UILocalizedIndexedCollation 的浅分析
- UILocalizedIndexedCollation 的浅分析
- UILocalizedIndexedCollation ios首字母排列
- 通讯录分组排序(UILocalizedIndexedCollation)
- 20160830UILocalizedIndexedCollation的使用
- UILocalizedIndexedCollation的使用
- iOS UILocalizedIndexedCollation排序神器
- iOS开发------通讯录分组排序(UILocalizedIndexedCollation)
- 关于使用UILocalizedIndexedCollation 实现tableview的边索引
- IOS开发(52)之UITableView索引搜索之UILocalizedIndexedCollation
- iOS 使用UILocalizedIndexedCollation实现通讯录样式的索引
- 本地化下按首字母分组排序的神器——UILocalizedIndexedCollation
- 本地化下按首字母分组排序的神器——UILocalizedIndexedCollation
- Webservice简单调用示例
- linux下无密码认证登录,总提示需要输入密码?
- Maven学习笔记
- 如何将ProE或者UG的装配体文件全盘导入Adams中(包括所有运动副)
- iOS开发——创建你自己的Framework
- UILocalizedIndexedCollation
- 237. Delete Node in a Linked List
- OAUTH协议 open auth 授权协议
- Spring入门Blog[八、Spring Aop织入点语法和相关案例总结]
- Java反射机制
- centos下一些软件安装
- Git常用命令笔记
- (一)Hive的安装部署及测试
- C语言