UILocalizedIndexedCollation

#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];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值