仿微信联系人分组(右侧索引)

1,创建模型:用来保存数据

创建模型:用来保存数据
模型.h文件

@interface CKUser : NSObject

//重写构造方法

-(instancetype)initWith:(NSString*)username name:(NSString*)name;

/*

 * 名字

 */

@property (assign, readwrite, nonatomic) NSString *name;

@property (assign, readwrite, nonatomic) NSString *username;

@end

模型.m文件

@implementation CKUser

-(instancetype)initWith:(NSString*)username name:(NSString*)name {

    self = [super init];

    self.username = username;

    self.name = name;

    

    return self;

}

@end


根控制器设置UITableViewController的控制器

#import "ViewController.h"

#import "CKUser.h"

@interface ViewController ()

//数据保存的模型数组

@property (nonatomic, strong) NSMutableArray *userArray;

//分组的数组

@property (nonatomic, strong) NSMutableArray *sectionsArray;

//UITableView索引搜索工具类

@property (nonatomic, strong) UILocalizedIndexedCollation *collation;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    //创建对象

//    self.collation = [UILocalizedIndexedCollation currentCollation];

    

    //拿到列表  A - Z 在加一个 一共 27

//    NSArray *temp = [self.collation sectionTitles];

    

//    NSLog(@"%@",temp);


    [self configureSections];

}


#define NEW_USER(str) [[CKUser alloc] initWith:str name:str]


//配置分组信息

- (void)configureSections {

    

    //初始化测试数据

    self.userArray = [[NSMutableArray alloc] init];

    

    [self.userArray addObject:NEW_USER(@"test001")];

    [self.userArray addObject:NEW_USER(@"test002")];

    [self.userArray addObject:NEW_USER(@"test003")];

    [self.userArray addObject:NEW_USER(@"test004")];

    [self.userArray addObject:NEW_USER(@"test005")];

    

    [self.userArray addObject:NEW_USER(@"admin01")];

    [self.userArray addObject:NEW_USER(@"admin02")];

    [self.userArray addObject:NEW_USER(@"admin03")];

    

    [self.userArray addObject:NEW_USER(@"bigBnag01")];

    [self.userArray addObject:NEW_USER(@"bigBnag02")];

    

    [self.userArray addObject:NEW_USER(@"what01")];

    [self.userArray addObject:NEW_USER(@"what02")];

    

    [self.userArray addObject:NEW_USER(@"李一")];

    [self.userArray addObject:NEW_USER(@"李二")];

    [self.userArray addObject:NEW_USER(@"刘涛")];

    [self.userArray addObject:NEW_USER(@"吕洞宾")];

     [self.userArray addObject:NEW_USER(@"刘麻子")];

    

    [self.userArray addObject:NEW_USER(@"曹操")];

    [self.userArray addObject:NEW_USER(@"曹值")];

    [self.userArray addObject:NEW_USER(@"大海")];


    

    [self.userArray addObject:NEW_USER(@"胡八一")];

    [self.userArray addObject:NEW_USER(@"胡八二")];

    

     [self.userArray addObject:NEW_USER(@"小三爷")];

    

      [self.userArray addObject:NEW_USER(@"周记包子")];

    //获得当前UILocalizedIndexedCollation对象并且引用赋给collation,A-Z的数据

    self.collation = [UILocalizedIndexedCollation currentCollation];

    

    

    //获得索引数和section标题数

    NSInteger index, sectionTitlesCount = [[self.collation sectionTitles] count];

    

    //临时数据,存放section对应的userObjs数组数据

    //容量为 27

    NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];

    

    //设置sections数组初始化:元素包含userObjs数据的空数据

    // newSectionsArray   里面添加了27个空数组

    for (index = 0; index < sectionTitlesCount; index++) {

        NSMutableArray *array = [[NSMutableArray alloc] init];

        [newSectionsArray addObject:array];

    }

    

    //将用户数据进行分类,存储到对应的sesion数组中

    //获取每一个 数据模型

    for (CKUser *userObj in self.userArray) {

        

        //根据timezonelocalename,获得对应的的section number

        //判断首字符 一个位置

        NSInteger sectionNumber = [self.collation sectionForObject:userObj collationStringSelector:@selector(username)];

        

        //获得section的数组

        NSMutableArray *sectionUserObjs = [newSectionsArray objectAtIndex:sectionNumber];

        

        //添加内容到section

        [sectionUserObjs addObject:userObj];

        

        //拿到当前 obj 所在的组数

        NSInteger index =  [self.collation sectionForObject:userObj collationStringSelector:@selector(username)];

        


    }

    

    

    

    //排序,对每个已经分类的数组中的数据进行排序,如果仅仅只是分类的话可以不用这步

    for (index = 0; index < sectionTitlesCount; index++) {

        

       //获取每个数组

        NSMutableArray *userObjsArrayForSection = [newSectionsArray objectAtIndex:index];

        

        //获得排序结果

        NSArray *sortedUserObjsArrayForSection = [self.collation sortedArrayFromArray:userObjsArrayForSection collationStringSelector:@selector(username)];

        

        //替换原来数组

        [newSectionsArray replaceObjectAtIndex:index withObject:sortedUserObjsArrayForSection];

    }

    

    self.sectionsArray = newSectionsArray;

}



//设置Section的数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

   

    

    return [[self.collation sectionTitles] count];


}


//设置每个Section下面的cell

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    

    // 在该节的时间区域的数目该节数组中的节相关联的数组的计数

    NSArray *UserObjsInSection = [self.sectionsArray objectAtIndex:section];

    

    return [UserObjsInSection count];

}

//设置每行的cell的内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    static NSString *Identifier = @"Cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];

    }

    

    // 从数组中的节索引关联的数组中获取区域。

    NSArray *userNameInSection = [self.sectionsArray objectAtIndex:indexPath.section];

    

    // 用时区的名称配置单元格

    CKUser *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有关的设定

 */

//设置sectionHeader

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NSArray *UserObjsInSection = [self.sectionsArray objectAtIndex:section];

    if(UserObjsInSection == nil || [UserObjsInSection count] <= 0) {

        return nil;

    }

    return [[self.collation sectionTitles] objectAtIndex:section];

}


//设置索引标题

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    return [self.collation sectionIndexTitles];

}


//关联搜索-点击跳转到对应组

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {


    

    //返回点击的 头部 索引

    return [self.collation sectionForSectionIndexTitleAtIndex:index];

}

@end




创建模型:用来保存数据
发布了41 篇原创文章 · 获赞 4 · 访问量 5万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章