OC -类似于通讯录的功能

一.建立person类包含name pinyin参数

#import "Person.h"

@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *pinYin;

二.实现类

#import "Person.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

<UITableViewDelegate,UITableViewDataSource>

{
    NSMutableArray *dataSource;
    UITableView *myTableView;
    NSMutableArray *titleAray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"UITableView";
    self.view.backgroundColor = [UIColor lightGrayColor];
    dataSource = [NSMutableArray array];
    NSArray *arr = [[NSArray alloc] initWithObjects:@"王达",@"李明",@"李松",@"朱子夫",@"秦始皇",@"刘彻",@"李世民",@"周树人",@"李松达",@"邓紫棋",@"安美",@"安菲",@"崔杰",@"冰冰",@"啊Q", nil];
    
    NSMutableArray *modelArray = [NSMutableArray array];
    for (int i = 0; i< arr.count; i++) {
        NSMutableString *mutableString = [NSMutableString stringWithString:arr[i]];
        CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
        CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformStripDiacritics, false);
        
        Person *model = [[Person alloc] init];
        model.name = arr[i];
        model.pinYin = mutableString;
        [modelArray addObject:model];
    }
    //NSSortDescriptor 指定用于对象数组排序的对象的属性
    NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"pinYin" ascending:YES]];
    
//对person进行排序
    [modelArray sortUsingDescriptors:sortDescriptors];  
    titleAray = [NSMutableArray array];
    //删除多余的字符
    for (int i = 0; i < modelArray.count; i++) {
        Person *model = modelArray[i];
        NSString *str = [model.pinYin substringToIndex:1];
        if (![titleAray containsObject:str]) {
            [titleAray addObject:str];
        }
    }
    //将中文数据进行二维的数组的拆分
    for (int i = 0; i< titleAray.count; i++) {
        NSString *str = titleAray[i];
        NSMutableArray *sortArray = [NSMutableArray array];
        for (Person *model in modelArray) {
            if ([model.pinYin hasPrefix:str]) {
                [sortArray addObject:model.name];
            }
        }
        [dataSource addObject:sortArray];
    }
    [self creatTableView];
}
#pragma mark - UITableViewDelegate
-(void)creatTableView{
    myTableView = [[UITableView alloc] initWithFrame:self.view.frame];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    //设置右侧索引的背景色
    myTableView.sectionIndexBackgroundColor = [UIColor clearColor];
    [self.view addSubview:myTableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [dataSource[section] count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSMutableArray *toBeReturned = [[NSMutableArray alloc]init];
    for(char c = 'A';c<='Z';c++)
        [toBeReturned addObject:[NSString stringWithFormat:@"%c",c]];
    return toBeReturned;
}
#pragma mark - UITableViewDataSource
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 20)];
    view.backgroundColor = [UIColor grayColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 60, 20)];
    NSString *str = titleAray[section];
    //转化为大写
    str = [str uppercaseString];
    label.text = str;
    [view addSubview:label];
    return view;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    }
    cell.textLabel.text = dataSource[indexPath.section][indexPath.row];
    return cell;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值