UI--折叠cell

之前在写share的时候遇到了折叠cell的问题,当时没有解决这个问题,回过头来看看如何实现cell的折叠


前言

这个cell是没有展开的样子
在这里插入图片描述
这个是cell点击展开后的样子
在这里插入图片描述


一、基本思路

  1. 使用数组将cell上需要显示的内容存储起来
  2. 定义一个TableViewCell来实现cell的内容显示
  3. 在UIVierwController上添加一个大小合适的UITableView,在这个UITableView上显示cell的内容
  4. 在UITableView的旁边添加一个UIButton到UIViewController上
  5. 点击UIButton的时候显示所有cell,button关闭的时候显示一行cell

二、代码

//
//  ViewController.h
//  折叠cell
//
//

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<UITableViewDelegate,
UITableViewDataSource>
@property (nonatomic, strong)UITableView* tableView;
//这里将Button和Array都写成了属性,在下边的代码中会解释原因
@property (nonatomic, strong)UIButton* button;
@property NSMutableArray* strArray;
@end




//  ViewController.m
//  折叠cell
//
//  Created by 王璐 on 2022/9/3.
//

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //tableview的初始化
    _tableView = [[UITableView alloc] init];
    _tableView.frame = CGRectMake(200,100,100,25);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"111"];
    
//向数组里添加需要显示的cell的内容
    _strArray = [[NSMutableArray alloc] init];
    [_strArray addObject:@"111"];
    [_strArray addObject:@"222"];
    [_strArray addObject:@"333"];
    [_strArray addObject:@"444"];
    
   //cell的折叠按钮
    _button = [[UIButton alloc] init];
    _button.frame = CGRectMake(300, 100, 30, 30);
    [_button setImage:[UIImage imageNamed:@"关闭.png"] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_button];
    // Do any additional setup after loading the view.
}
//是否折叠的按钮点击函数
- (void)pressBtn{
    if (_button.selected == NO){
        self.tableView.frame = CGRectMake(200, 100, 100, 100);
        [_button setImage:[UIImage imageNamed:@"打开.png"] forState:UIControlStateNormal];
        _button.selected = YES;
    } else{
        self.tableView.frame = CGRectMake(200, 100, 100, 25);
        [_button setImage:[UIImage imageNamed:@"关闭.png"] forState:UIControlStateNormal];
        _button.selected = NO;
    }
    [_tableView reloadData];
}
//cell的行数
//这里用到了button,所以这个button必须写成属性
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_button.selected == NO){
        return 1;
    } else{
        return 5;
    }
}
//cell的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 25;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewcell* cell = [tableView dequeueReusableCellWithIdentifier:@"111"];
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"111"];
    }
    //这里需要把数组的内容显示到cell上,所以这个数组也需要是属性
    cell.textLabel.text = _strArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:20];
    return cell;
}
@end

添加相对应地图片后即可实现cell的折叠。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山河丘壑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值