小tableView点击按钮进行展开和折叠

本文介绍了一种使用 iOS 中的 UITableView 实现动态展开和收起效果的方法。通过一个可变数组存储数据,结合一个布尔变量判断 tableView 的状态,从而调整单元格的行数和位置大小。适合初学者了解 UITableView 的基本操作。

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

我也不知道,这个应该叫什么,反正,先看一下效果图吧
弄出的效果就是这个样子
刚开始的状态,右边是一个按钮,点击后可以展开
点击右边按钮后展开。
在这里插入图片描述
然后点击单元格进行选择,选择后再次合上,显示的是选择的内容,然后可以再次点击按钮,重新进行选择
在这里插入图片描述
接下来,看看这个是怎么实现的,应该有很多的方法,我是个初学者,所以目前只会这一种。

  • 一个可变数组,里面存储,这些文字
  • 一个全局变量,布尔值,用来判断是tableView的状态
  • 然后根据布尔值,来设置单元格的行数,和位置大小
//设置初始的tableView
	_tableview = [[UITableView alloc] init];
    _tableview.frame = CGRectMake(260, 160, 120, 30);
    [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"littleCell"];
    [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"little2Cell"];

    [self.view addSubview:_tableview];

    _tableview.dataSource = self;
    _tableview.delegate = self;
//这是存储数据的可变数组数组
	_dataArray  = [[NSMutableArray alloc] init];
    [_dataArray addObject:@"原创作品"];
    [_dataArray addObject: @"设计资料"];
    [_dataArray addObject:@"设计师观点"];
    [_dataArray addObject:@"设计教程"];入代码片

.......

//根据不同的布尔值,返回不同的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_isOpen) {
        UITableViewCell *cell = [_tableview dequeueReusableCellWithIdentifier:@"little2Cell" forIndexPath:indexPath];
        cell.textLabel.text = _dataArray[indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:12];
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
        return cell;
    } else {
        UITableViewCell *cell = [_tableview dequeueReusableCellWithIdentifier:@"littleCell" forIndexPath:indexPath];
        cell.textLabel.text = _dataArray[indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:12];
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
        return cell;
    }
   
}


//设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_isOpen) {
        return 4;
    } else {
        return 1;
    }
    return 4;
}


//button的点击事件,点击后要改变布尔值
-(void)pressOpen {
    if (_isOpen) {
        _tableview.frame = CGRectMake(260, 160, 120, 30);
        _isOpen = NO;
    } else {
        
        _tableview.frame = CGRectMake(260, 160, 120, 120);
        _isOpen = YES;
    }
    [_tableview reloadData];   //数据更新,一定不要忘记写这一句,不然,可以试试
}


//点击单元格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _str =  [NSString stringWithString:_dataArray[indexPath.row]];
    [_dataArray insertObject:_str atIndex:0];								//将选中的对应的数据插入到0的位置处,
    [_dataArray removeObjectAtIndex:indexPath.row + 1];			//删除选中的数据
    _isOpen = NO;																		//改变布尔值
    _tableview.frame = CGRectMake(260, 160, 120, 30);			//重新设定位置大小
    [_tableview reloadData];															//更新数据
}

最后就可以实现想要的效果了,刚开始学习,新手一枚,若有错误,请及时纠正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值