iOS开发之模仿qq通讯录源代码!

这篇文章主要整理一下项目中用到的类似qq通讯录的收缩功能。
图片描述

我实现的思路是在tableview的header放置button,然后根据button是否选中来判断是否需要显示那一个section。

首先定义了两个属性

@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSArray *buttonsArr;

button数组的定义

-(NSArray *)buttonsArr{

    if (_buttonsArr==nil) {

        NSMutableArray *tmp=[NSMutableArray array];

        for (int i=0; i<6; i++) {

            UIButton *button=[UIButton buttonWithType:
UIButtonTypeCustom];
            button.tag=333+i;
            button.backgroundColor=[UIColor redColor];
            [button setTitle:[NSString stringWithFormat:
@"%d",i] forState:UIControlStateNormal];
            button.selected=NO;
            [button addTarget:self action:@selector
(buttonAction:) forControlEvents:
UIControlEventTouchUpInside];

            [tmp addObject:button];
        }
        _buttonsArr=[NSArray arrayWithArray:tmp];
    }
    return _buttonsArr;
}

按钮的点击方法,根据按钮的选中状态刷新tableview

-(void)buttonAction:(UIButton *)button{

    button.selected=!button.isSelected;

    [self.tableview reloadSections:[NSIndexSet 
indexSetWithIndex:button.tag-333] withRowAnimation:
UITableViewRowAnimationFade];
}

tableview的一系列代理方法

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

    return self.buttonsArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section{

    UIButton *button=self.buttonsArr[section];

    if (button.selected==YES) {
        return 1;
    }else{
        return 0;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView
 cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *str=@"cellid";

    UITableViewCell *cell=[tableView 
dequeueReusableCellWithIdentifier:str];

    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:str];
        cell.textLabel.text=[NSString stringWithFormat:
@"第%li节",indexPath.section];
    }

    return cell;
}
-(UIView *)tableView:(UITableView *)tableView
 viewForHeaderInSection:(NSInteger)section{

    UIButton *button=self.buttonsArr[section];

    return button;
}
-(CGFloat)tableView:(UITableView *)tableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 150;
}
-(CGFloat)tableView:(UITableView *)tableView 
heightForHeaderInSection:(NSInteger)section{

    return 50;
}

原文地址:http://www.code4app.com/blog-...

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值