iOS 使用UILocalizedIndexedCollation实现通讯录样式的索引
来源:互联网 发布:unity3d 对象数组 编辑:程序博客网 时间:2024/04/30 18:36
前一段时间要在应用程序中要加入检索的功能,在网上找了很多是手写的,于是今天在这里写一下备忘。直接贴的项目代码。
//*************增加排序和索引**************8*
addressBookArray=[NSMutableArrayarrayWithCapacity:1];
UILocalizedIndexedCollation *indexedCollation=[UILocalizedIndexedCollationcurrentCollation];
for (ISSTUserModel*theAdressBookModel in addressBookArrayTmp) {
NSInteger section=[indexedCollationsectionForObject:theAdressBookModelcollationStringSelector:@selector(name)];
theAdressBookModel.section=section;//这里的addressModel是一个类,里面有name属性,这个方法是按照name来分区。
}
NSInteger sectionCount=[[indexedCollationsectionTitles]count];
NSMutableArray *sectionsArray=[NSMutableArrayarrayWithCapacity:sectionCount];//这里使用的是二维数组,这是外面一层
for (inti=0; i<=sectionCount; ++i) {
NSMutableArray *singleSectionArray=[NSMutableArrayarrayWithCapacity:1];//这是在每个外层数组里面初始化子数组
[sectionsArray addObject:singleSectionArray];
}
for(ISSTUserModel*theAdressBookModel in addressBookArrayTmp){//将对象放到正确的区段当中
[(NSMutableArray*) [sectionsArray objectAtIndex:theAdressBookModel.section]addObject:theAdressBookModel];
}
for (NSMutableArray*singleSectionArray in sectionsArray){//排序子数组中的数据
NSArray *sortedSection=[indexedCollationsortedArrayFromArray:singleSectionArraycollationStringSelector:@selector(name)];
[addressBookArrayaddObject:sortedSection];
}
{
// Return the number of sections.
return [addressBookArraycount];
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView==self.searchController.searchResultsTableView)
return [searchArraycount];
else
return [[addressBookArrayobjectAtIndex:section]count];
// return studyArray.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell=(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellIdentifier];
}
if(tableView==self.searchController.searchResultsTableView)
cell.textLabel.text=[searchArrayobjectAtIndex:indexPath.row];
else{
ISSTUserModel *adressBook=[[addressBookArrayobjectAtIndex:[indexPathsection]]objectAtIndex:[indexPathrow]];
cell.textLabel.text= adressBook.name;//[namesArray objectAtIndex:indexPath.row];
}
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;
if ([[addressBookArrayobjectAtIndex:section]count] >0) {
return [[[UILocalizedIndexedCollationcurrentCollation]sectionTitles]objectAtIndex:section];
}
return nil;
return [[UILocalizedIndexedCollationcurrentCollation]sectionIndexTitles];
return [[UILocalizedIndexedCollationcurrentCollation]sectionForSectionIndexTitleAtIndex:index];
}
@property (retain,nonatomic) NSArray *filteredProducts;
@property (strong,nonatomic) UISearchBar* searchBar;
@property (strong,nonatomic) UISearchDisplayController* searchController;
// Create search bar
self.searchBar = [[UISearchBaralloc] initWithFrame:
CGRectMake(0.0f,0.0f, 320.0f,44.0f)];
self.tableView.tableHeaderView =self.searchBar;
初始化搜索框和搜索控制器。
// Create and configure the search controller
self.searchController = [[UISearchDisplayControlleralloc]
initWithSearchBar:self.searchBar
contentsController:self];
self.searchController.searchResultsDataSource =self;
self.searchController.searchResultsDelegate =self;
NSMutableArray *flattenedArray = [[NSMutableArrayalloc]
initWithCapacity:1];
for (NSMutableArray *theArrayin self.products)
{
for (int i=0; i<[theArraycount];i++)
{
[flattenedArray addObject:[theArrayobjectAtIndex:i]];
}
}
//构造谓词进行筛选,赋值给filteredProduct,那么接下来只要在搜索框的tableview里显示数据就可以了。
// Set up an NSPredicate to filter the rows
NSPredicate *predicate = [NSPredicatepredicateWithFormat:
@"name beginswith[c] %@",self.searchBar.text];
self.filteredProducts = [flattenedArray
filteredArrayUsingPredicate:predicate];
- iOS 使用UILocalizedIndexedCollation实现通讯录样式的索引
- 关于使用UILocalizedIndexedCollation 实现tableview的边索引
- iOS开发------通讯录分组排序(UILocalizedIndexedCollation)
- 20160830UILocalizedIndexedCollation的使用
- UILocalizedIndexedCollation的使用
- iOS通讯录旁边的索引表的实现
- iOS UILocalizedIndexedCollation
- 通讯录分组排序(UILocalizedIndexedCollation)
- UILocalizedIndexedCollation 使用
- IOS开发(52)之UITableView索引搜索之UILocalizedIndexedCollation
- iOS关于通讯录的实现
- 通讯录索引条的功能实现
- IOS系统通讯录的使用
- UILocalizedIndexedCollation ios首字母排列
- iOS UILocalizedIndexedCollation排序神器
- Andorid使用自定义View实现通讯录,媒体文件等等的ListView的字母索引
- ios实现通讯录的查询与删除
- ios实现通讯录的查询与删除
- RTH(Randomized Hough Transform)中计算p1,p2,p3三个点确定的平面在参数空间的坐标
- Java 中与时间有关的几个问题
- string类型
- android修改settings源码隐藏部分布局
- openssl-1.0.2-beta3 mips 平台交叉编译
- iOS 使用UILocalizedIndexedCollation实现通讯录样式的索引
- UVa 10935 - Throwing cards away I
- 手机扫描二维码登录原理
- arcgis for js 修改鼠标在新增点上的指针样式
- Java读写properties配置文件
- 有符号右移,无符号右移,左移
- CentOS 6.6下编译安装MySQL-5.6.21
- JZMQ
- vector类型