iOS-伸縮表實(shí)現(xiàn)(QQ列表模式)

1.聲明屬性和代理
    <UITableViewDataSource>
    {
        BOOL _flagArr[100];
        NSArray *_array;
    }
    @property(nonatomic,strong)UITableView *tableView;

2.創(chuàng)建tableview
    - (void)createTableView{
        _array = @[@"驢友",@"牌友黨",@"大學(xué)同學(xué)",@"非好友"];
        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.實(shí)現(xiàn)代理和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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容