UILocalizedIndexedCollation

来源:互联网 发布:网络嗅探1.6 编辑:程序博客网 时间:2024/04/30 16:47
#pragma mark -------配置分组信息------
#define NEW_USER(str) [[User alloc] init:str name:str]
//配置分组信息
- (void)configureSections {
     
    //初始化测试数据
    userArray = [[NSMutableArrayalloc] 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 = [UILocalizedIndexedCollationcurrentCollation];
    //获得索引数和section标题数
    NSIntegerindex, sectionTitlesCount = [[collation sectionTitles] count];
     
    //临时数据,存放section对应的userObjs数组数据
    NSMutableArray*newSectionsArray = [[NSMutableArrayalloc] initWithCapacity:sectionTitlesCount];
     
    //设置sections数组初始化:元素包含userObjs数据的空数据
    for(index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray*array = [[NSMutableArrayalloc] init];
        [newSectionsArray addObject:array];
    }
     
    //将用户数据进行分类,存储到对应的sesion数组中
    for(User *userObj in userArray) {
         
        //根据timezone的localename,获得对应的的section number
        NSIntegersectionNumber = [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 {
     
    staticNSString *CellIdentifier = @"Cell";
     
    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier: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;
     
    returncell;
}
- (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) {
        returnnil;
    }
    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
原创粉丝点击