【iOS开发】- UITableView-折叠Cell

前言

  • 在暑期学习完成3Gshare的时候有一个界面需要使用折叠cell,在这里把那部分的折叠cell详细解释一下

3Gshare的引入

请添加图片描述

  • 如图所示,点开展示折叠cell的内容,收起展示点击的选项即可

思路

  • 上面展示的是把展开button和收起button做成cell放上去,这里做成imageButton。
  • 设置一个存放内容的数组arrayCell,设置一个tableView,一个按钮ButtonChange,一个当前CellcellCure
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *ButtonChange;
@property (nonatomic, strong) NSMutableArray *arrayCell;
@property (nonatomic, strong) UITableViewCell *cellCure;

  • 点击ButtonChange
    • 改变tableView的宽度,并且改变图标(记得更新数据)
- (void)pressButton :(UIButton*) button{
    if (_ButtonChange.tag == 1) {
        _tableView.frame = CGRectMake(230, 180, 120, 120);
        [_ButtonChange setImage:[UIImage imageNamed:@"buttonno.png"] forState:UIControlStateNormal];
        _ButtonChange.tag = 2;
        [_tableView reloadData];
    } else {
        _tableView.frame = CGRectMake(230, 180, 120, 35);
        [_ButtonChange setImage:[UIImage imageNamed:@"buttonyes.png"] forState:UIControlStateNormal];
        _ButtonChange.tag = 1;
        [_tableView reloadData];
    }
}
  • 点击当前cell改变内容
  • 请添加图片描述
    请添加图片描述
    请添加图片描述
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 获取当前点击的cell的label的text
    _cellCure = [_tableView cellForRowAtIndexPath:indexPath];
    NSString* string = [NSString stringWithString:_cellCure.textLabel.text];
    for (int i = 0; i < _arrayCell.count; i ++) {
        if ([string isEqualToString:_arrayCell[i]]) {
            _arrayCell[i] = _arrayCell[0];
            break;
        }
    }
    _arrayCell[0] = string;
    _tableView.frame = CGRectMake(230, 180, 120, 25);
    [_ButtonChange setImage:[UIImage imageNamed:@"buttonyes.png"] forState:UIControlStateNormal];
    _ButtonChange.tag = 1;
    [_tableView reloadData];
}
  • 注意对于上述代码的
    for (int i = 0; i < _arrayCell.count; i ++) { if ([string isEqualToString:_arrayCell[i]]) { _arrayCell[i] = _arrayCell[0]; break; } }部分
      • 是为了保持数组里面的元素不变,达成一泓交换,相当于把当前cell的内容赋值给选择的cell,然后选择的cell就会跑到第一个cell的位置,之后折叠即可显示选择的cell

完整代码

@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *ButtonChange;
@property (nonatomic, strong) NSMutableArray *arrayCell;
@property (nonatomic, strong) UITableViewCell *cellCure;

@end


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(230, 180, 120, 25) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"1"];
    [self.view addSubview:_tableView];
    
    _ButtonChange = [[UIButton alloc] init];
    _ButtonChange.frame = CGRectMake(200, 180, 30, 30);
    [_ButtonChange setImage:[UIImage imageNamed:@"buttonyes.png"] forState:UIControlStateNormal];
    [_ButtonChange addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
    _ButtonChange.tag = 1;
    [self.view addSubview:_ButtonChange];
    
    _arrayCell = [NSMutableArray arrayWithObjects:@"iOS", @"macOS", @"3G", @"Cell", nil];
    
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"1" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor orangeColor];
    cell.textLabel.text = _arrayCell[indexPath.row];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_ButtonChange.tag == 1) {
        return 1;
    } else {
        return 4;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 获取当前点击的cell的label的text
    _cellCure = [_tableView cellForRowAtIndexPath:indexPath];
    NSString* string = [NSString stringWithString:_cellCure.textLabel.text];
    for (int i = 0; i < _arrayCell.count; i ++) {
        if ([string isEqualToString:_arrayCell[i]]) {
            _arrayCell[i] = _arrayCell[0];
            break;
        }
    }
    _arrayCell[0] = string;
    _tableView.frame = CGRectMake(230, 180, 120, 25);
    [_ButtonChange setImage:[UIImage imageNamed:@"buttonyes.png"] forState:UIControlStateNormal];
    _ButtonChange.tag = 1;
    [_tableView reloadData];
}
- (void)pressButton :(UIButton*) button{
    if (_ButtonChange.tag == 1) {
        _tableView.frame = CGRectMake(230, 180, 120, 120);
        [_ButtonChange setImage:[UIImage imageNamed:@"buttonno.png"] forState:UIControlStateNormal];
        _ButtonChange.tag = 2;
        [_tableView reloadData];
    } else {
        _tableView.frame = CGRectMake(230, 180, 120, 35);
        [_ButtonChange setImage:[UIImage imageNamed:@"buttonyes.png"] forState:UIControlStateNormal];
        _ButtonChange.tag = 1;
        [_tableView reloadData];
    }
}
@end

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值