QQ列表的展开收起

RootViewController.h

@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> {

    BOOL Close[15]; //用于存放每一组的收起展开状态    YES 是收起  NO是展开
    UITableView *_tableView;
}

@property(nonatomic, retain)NSArray *data;

RootViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    //创建表视图
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain];
    //设置代理
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.sectionHeaderHeight = 44;
    
    [self.view addSubview:_tableView];
   
    //查找文件的路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"font" ofType:@"plist"];
    _data = [[NSArray alloc] initWithContentsOfFile:path];
}

/*
 _data数据存放的格式
 [
 [@"字体1",@"字体2",@"字体3",@"字体4",@"字体5"],
 [@"字体1",@"字体2",@"字体3"],
 [@"字体1",@"字体2"@"字体5"],
 [@"字体1",@"字体2",@"字体4",@"字体5"],
 ....
 ]
 */

#pragma mark - UITableView dataSource

//设置表视图的组的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return _data.count;
}

//设置每一组cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    //取得对应组里面的元素
    NSArray *arrary2D = _data[section];
    
    BOOL isClose = Close[section];
    
    if (isClose == NO) {
        return arrary2D.count;
    }
    
    return 0;
    
}

/*
 _data数据存放的格式
 [
 [@"字体1",@"字体2",@"字体3",@"字体4",@"字体5"],
 [@"字体1",@"字体2",@"字体3"],
 [@"字体1",@"字体2"@"字体5"],
 [@"字体1",@"字体2",@"字体4",@"字体5"],
 ....
 ]
 */

//创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *iden = @"cell110";
    
    //从闲置池中去cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease];
    }
    
    NSArray *arrary2D = [_data objectAtIndex:indexPath.section];
    NSString *name = [arrary2D objectAtIndex:indexPath.row];
    
    //给cell添加数据
    cell.textLabel.text = name;
    cell.textLabel.font = [UIFont fontWithName:name size:17];
    
    return cell;
    
}

//设置组的头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    NSString *name = [NSString stringWithFormat:@"好友分组%d",section];
    
    //创建按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置按钮的背景图片
    [button setBackgroundImage:[UIImage imageNamed:@"tableCell_common"] forState:UIControlStateNormal];
    //设置按钮的标题
    [button setTitle:name forState:UIControlStateNormal];
    button.tag = section;
    //设置按钮显示的标题颜色
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
    
}

//按钮的点击事件
- (void)buttonAction:(UIButton *)button {

    int section = button.tag;
    <strong>//将标示取反</strong>
    Close[section] = !Close[section];
    
    //刷新单元格
//    [_tableView reloadData];
    
    //刷新特定的组
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];
    
    [_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值