iOS-伸缩表实现(QQ列表模式)

1.声明属性和代理
    
    {
        BOOL _flagArr[100];
        NSArray *_array;
    }
    @property(nonatomic,strong)UITableView *tableView;

2.创建tableview
    - (void)createTableView{
        _array = @[@"驴友",@"牌友党",@"大学同学",@"非好友"];
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 72+HEIGHT/16+10, WIDTH, 2)];
        line.backgroundColor = SLIVERYCOLOR;
        [self.view addSubview:line];
        
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 72+HEIGHT/16+10+2, WIDTH, HEIGHT-84-HEIGHT/16)];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.rowHeight = 50;
        [self.view addSubview:self.tableView];

}

3.实现代理和datasource方法
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return _array.count;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return HEIGHT/14;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/14)];
    sectionView.backgroundColor = [UIColor whiteColor];
    [self.tableView addSubview:sectionView];

    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT/14-2, WIDTH, 2)];
    line.backgroundColor = SLIVERYCOLOR;
    [sectionView addSubview:line];
    
    UIImageView * arrow = [[UIImageView alloc]initWithFrame:CGRectMake(15, (HEIGHT/14-15)/2, 15, 15)];
    [arrow setImage:[UIImage imageNamed:@"contacts_arrow.png"]];
    if (_flagArr[section]) {
        arrow.transform = CGAffineTransformMakeRotation(M_PI_2);
    }else{
        arrow.transform = CGAffineTransformIdentity;
    }
    [sectionView addSubview:arrow];
    
    UILabel *groupLab = [[UILabel alloc]initWithFrame:CGRectMake(40, 0, WIDTH/2-35, HEIGHT/14)];
    groupLab.text = _array[section];
    groupLab.textColor = TEXTCOLOR;
    groupLab.font = [UIFont systemFontOfSize:13];
    groupLab.textAlignment = NSTextAlignmentLeft;
    [sectionView addSubview:groupLab];
    
    UILabel *numLab = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/2, 0, WIDTH/2-43, HEIGHT/14)];
    numLab.text = @"8/13";
    numLab.textColor = TEXTCOLOR;
    numLab.font = [UIFont systemFontOfSize:13];
    numLab.textAlignment = NSTextAlignmentRight;
    [sectionView addSubview:numLab];
    
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(WIDTH-33, (HEIGHT/14-18)/2, 18, 18);
    deleteBtn.layer.cornerRadius = 9;
    deleteBtn.clipsToBounds = YES;
    [deleteBtn setImage:[UIImage imageNamed:@"fenzu.png"] forState:UIControlStateNormal];
    deleteBtn.adjustsImageWhenHighlighted = NO;
    [deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [sectionView addSubview:deleteBtn];

    UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    clickBtn.frame = CGRectMake(0, 0, WIDTH-35,HEIGHT/14);
    clickBtn.layer.cornerRadius = 9;
    clickBtn.tag = 180 + section;
    clickBtn.clipsToBounds = YES;
    clickBtn.adjustsImageWhenHighlighted = NO;
    [clickBtn addTarget:self action:@selector(clickBtnBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [sectionView addSubview:clickBtn];
    
    return sectionView;
}

- (void)deleteBtnClick:(UIButton *)sender{
    
}

- (void)clickBtnBtnClick:(UIButton *)sender{
    _flagArr[sender.tag-180] = !_flagArr[sender.tag-180];

    [self.tableView reloadData];
}


- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_flagArr[section]) {
        return 3;
    }else{
        return 0;
    }
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"ForbidGroupTableViewCell";
    ForbidGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:cellID owner:self options:nil]lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    [cell.deleBtn setImage:[UIImage imageNamed:@"fenzu.png"] forState:UIControlStateNormal];
    [cell.headImage setImage:[UIImage imageNamed:@"wb_binding.png"]];
    cell.nameLab.text = @"呵呵";
    cell.signLab.text = @"阿弥陀佛";

    return cell;
}

你可能感兴趣的:(iOS-伸缩表实现(QQ列表模式))