iOS学习笔记--自定义通讯录

最近在项目中遇到这样一个问题,在自定义的通讯录中点击索引的时候对应的分组标题改变其状态(如改变其字体颜色、类型、字号等)。在网上找了很长时间但没找到可以使用的,这里有一个写的可以用的拿出来分享一下,写的不好还请见谅!

主要思路:

一、在工程中通过对数据源进行处理,使它们按照汉语拼音进行排序并按照拼音的第一个字母进行分组。

如:现在有数据源
   self.tmpNameArr = [NSArray arrayWithObjects:@"Aorde",@"爱旅行的猫",@"张三丰",@"张道陵",@"李太白",@"八大山人",@"温庭筠",@"杜甫",@"王安石",@"李白",@"张三",@"重庆",@"重量",@"调节",@"调用",@"小白",@"小明",@"千珏",@"黄家驹", @"鼠标",@"hello",@"多美丽",@"肯德基",@"##",@"e才到家",@"方米",@"高明",@"icd",@"jeer",@"米杰",@"宁宁",@"盟们",@"彭明",@"任港南飞",@"他宝宝",@"袁浩",nil];
   将它们进行分组后,建立2个数组,一个是排序后出现过得品应首字母数组也就是索引数组(indexArray),一个是排序号的结果数组(letterResultArr)。在这里排序分组用到了网上的ZYPinYinSearchLib 和 BMChineseStringSort。

二、用tableView来展示

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

        return self.indexArray[section];
}
//section的titleHeader的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return MDXFrom6(15);
}
//section行数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

     return self.indexArray.count;

}
//每组section个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [self.letterResultArr[section] count];

}
//section右侧index数组
//右侧索引列表
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
       return self.indexArray;
}
-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * tongXunLuID = @"tongXunLuID";

    tongXuLuTableViewCell * tongXunLuCell = [tableView dequeueReusableCellWithIdentifier:tongXunLuID];

    if (!tongXunLuCell)
    {
        tongXunLuCell = [[tongXuLuTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:tongXunLuID];

        tongXunLuCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
           Person *p = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

        [tongXunLuCell.tmpNameLabel setText:[NSString stringWithFormat:@"%@",p.name]];
    return tongXunLuCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return MDXFrom6(49);
}
#pragma mark -- tableView点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{//这里是tableViewcell 的点击事件
}

三、点击索引时改变对应section的状态

定义一个数组arr,在数据源分组完成后初始化并赋值
    self.arr = [NSMutableArray array];
    for (NSInteger i=0; i<self.letterResultArr.count; i++)
    {
        [self.arr addObject:@"1"];
    }
设置滑动代理
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        [self.arr removeAllObjects];
        for (NSInteger i=0; i<self.letterResultArr.count; i++) {

            [self.arr addObject:@"1"];
        }
}
设置点击索引的操作
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{

    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] atScrollPosition:UITableViewScrollPositionTop animated:YES];

    [tableView reloadData];
    NSMutableArray *ar = [NSMutableArray arrayWithArray:self.arr];
    [self.arr removeAllObjects];

//标记    
    for (NSInteger i=0; i<ar.count; i++)
    {
        if (i == index)
        {
            [self.arr addObject:@"2"];
        }
        else
        {
            [self.arr addObject:@"1"];
        }

    }
    return index;
}

在自定义的view中改变状态
//section 自定义
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* myView = [[UIView alloc] init];
    myView.backgroundColor = RGB_Color(255, 255, 255);
    self.sectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, KDeviceWidth, MDXFrom6(15))];
    self.sectionLabel.textColor=[UIColor blackColor];
    self.sectionLabel.backgroundColor = RGB_Color(181, 181, 181);
    [self.sectionLabel setTextAlignment:NSTextAlignmentCenter];
    self.sectionLabel.text=[self.indexArray objectAtIndex:section];


    NSString *mod = self.arr[section];
    if ([mod integerValue] == 2)
     {
       [self.sectionLabel setTextColor:[UIColor redColor]];
     }
    [myView addSubview:self.sectionLabel];

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值