仿qq列表Tableview伸缩展示

本文展示了如何在iOS应用中通过UITableView模仿QQ列表的伸缩展示效果。通过控制数组来标记列表的展开和关闭状态,实现点击表头切换展开与关闭,并更新表格视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下为基本展示,若有其他需求,可根据需求自行调试

 

#import "ViewController.h"

 

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

{

    UITableView *detailTableView;

    NSMutableArray *selectedArr;//控制列表是否被打开(数组中是否包含该章节,若包含,则关闭,反则展开)

    NSMutableArray *_sectionArray;//章节array

    NSMutableArray *cellArray;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    selectedArr=[[NSMutableArrayalloc]init];

    _sectionArray = [[NSMutableArrayalloc]initWithObjects:@"章节1",@"章节2",@"章节3",@"章节4",@"章节5",nil];

    cellArray = [[NSMutableArrayalloc]initWithObjects:@"cell1",@"cell2",@"cell3",nil];

    [self creatUI];

    //设置默认全关闭状态

    for(int i =0; i < [_sectionArraycount]; i ++)

    {

        [selectedArraddObject:[NSStringstringWithFormat:@"%ld",(long)i]];

    }

}

#pragma mark TableVIewDelegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    

}

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

    

    NSString *indexStr = [NSStringstringWithFormat:@"%ld",(long)section];

    //只需要给当前分组展开的section添加用户信息即可

    if ([selectedArrcontainsObject:indexStr]) {

        return 0;

    }else

    {

        return [cellArraycount];

    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 51;

}

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

    static NSString *cellIndentifier =@"foundCityCellDetail";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];

    if (cell == nil) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIndentifier];

        cell.selectionStyle =UITableViewCellSelectionStyleNone;

    }

    NSString *indexStr = [NSStringstringWithFormat:@"%ld",(long)indexPath.section];

    //只需要给当前分组展开的section添加用户信息即可

    if (![selectedArrcontainsObject:indexStr]) {

        cell.textLabel.text = [cellArrayobjectAtIndex:indexPath.row];

    }

    return cell;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [_sectionArraycount];

}

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

{

    return 44;

}

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

{

    UIView *headerVIew = [[UIViewalloc]initWithFrame:CGRectMake(0,0, tableView.frame.size.width,44)];

    headerVIew.backgroundColor = [UIColorwhiteColor];

    

    UIImageView *imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(5,15, 15, 15)];

    imageView.tag=20000+section;

    //更具当前是否展开设置图片

    NSString *string = [NSStringstringWithFormat:@"%ld",(long)section];

    if ([selectedArrcontainsObject:string]) {

        imageView.image=[UIImageimageNamed:@"grayNarrowRight"];

    }else{

        imageView.image=[UIImageimageNamed:@"grayNarrowDown"];

    }

    [headerVIew addSubview:imageView];

    

    UILabel *titleLabel=[[UILabelalloc]initWithFrame:CGRectMake(imageView.frame.origin.x + imageView.frame.size.width +10, 0, 170, headerVIew.frame.size.height)];

    [headerVIew addSubview:titleLabel];

 

    

    //添加一个button用来监听点击分组,实现分组的展开关闭。

    UIButton *btn=[UIButtonbuttonWithType:UIButtonTypeCustom];

    btn.frame=CGRectMake(0,0, tableView.frame.size.width -80, 50);

    btn.tag=10000+section;

    [btn addTarget:selfaction:@selector(btnOpenList:)forControlEvents:UIControlEventTouchDown];

    [headerVIew addSubview:btn];

    

    UILabel *botLine = [[UILabelalloc]initWithFrame:CGRectMake(0,43.5, self.view.frame.size.width,0.5)];

    botLine.backgroundColor =[UIColorgrayColor];

    [headerVIew addSubview:botLine];

    

    titleLabel.text = [_sectionArrayobjectAtIndex:section];

    

    return headerVIew;

}

#pragma mark 章节section点击

-(void)btnOpenList:(UIButton *)sender

{

    NSString *string = [NSStringstringWithFormat:@"%ld",(long)sender.tag-10000];

    //数组selectedArr里面存的数据和表头想对应,方便以后做比较

    if ([selectedArrcontainsObject:string])

    {

        [selectedArrremoveObject:string];

    }

    else

    {

        [selectedArraddObject:string];

    }

    [detailTableViewreloadData ];

}

#pragma mark -

#pragma mark 绘图

-(void) creatUI

{

    self.view.backgroundColor = [UIColorwhiteColor];

    detailTableView = [[UITableViewalloc] initWithFrame:CGRectZerostyle:UITableViewStylePlain];

    detailTableView.dataSource =self;

    detailTableView.delegate =self;

    detailTableView.separatorStyle =UITableViewCellSeparatorStyleNone;

    detailTableView.backgroundColor = [UIColorwhiteColor];

    detailTableView.tableFooterView = [[UIViewalloc]init];

    [self.viewaddSubview:detailTableView];

    detailTableView.frame =CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height);

}

 

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值