QQ分组效果

tableView简单实现qq分组的效果

定义一个BOOL类型的数组,count要大于要分的组数


首先定义有多少组

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


    return _data.count;

}


//返回相应组里面有多少个单元格

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


    NSArray *array2D = _data[section];

    

    BOOL isClose = close[section];

    

    if (isClose) {

        return 0;

    }

    

    return array2D.count;

    

}


//创建每一个单元格

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


    static NSString *iden = @"cell";

    

    //从闲置池中取单元格

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];

    

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease];

    }

    

    /*data的样式:

     [

     [@"字体1"@"字体2"@"字体3"@"字体4"@"字体5"],  第一组

     [@"字体1"@"字体2"@"字体3"],    第二组

     [@"字体1"@"字体2"@"字体3"@"字体4"],

     ....

     ]

     */

    

    //取得组

    NSInteger section = indexPath.section;

    NSArray *array2D = _data[section];

    

    //添加数据

    cell.textLabel.text = array2D[indexPath.row];

    

    return cell;

}

通过代理方法设置头视图,需要确定组的头视图高度,否则不会显示头视图

//设置组的头视图

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {


    //创建按钮

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];


    //设置tag

    button.tag = section;

    NSString *title = [NSString stringWithFormat:@"好友分组%ld",section];

    [button setTitle:title forState:UIControlStateNormal];

    [button setBackgroundImage:[UIImage imageNamed:@"tableCell_common"] forState:UIControlStateNormal];

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    

    return button;

    

}

button的点击事件

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


    //取得点击的组

    NSInteger section = button.tag;

    

    close[section] = !close[section];

    

    //刷新视图

//    [_tableView reloadData];

    

    //刷新指定的组

    

    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];

    

    [_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];

    

    

    

}

//设置组的头视图的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {


    return 44;

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值