iOS——折叠cell

在暑假的项目里我已经写过了折叠cell,但是那个cell的内容的变更只是使用label,因此这次我又一次学习了折叠cell,使用了可变数组来改变cell的单元格的内容,这样,通过点击单元格就可以实现cell的打开与关闭。

首先,我创建了两个属性,一个单元格一个可变数组,然后单元格的内容是按照数组的元素顺序排列的,当点击单元格的时候,实际上是改变了数组的元素顺序,来以此改变单元格的内容。

代码如下:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.arr = [NSMutableArray arrayWithObjects:@"a", @"b", @"c", @"d", @"e", nil];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.frame = CGRectMake(30, 100, 250, 100);
    self.tableView.pagingEnabled = NO;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"id"];
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"id"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.textLabel.text = self.arr[indexPath.row];
    cell.backgroundColor = [UIColor orangeColor];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        if (self.tableView.bounds.size.height == 100) {
            self.tableView.frame = CGRectMake(30, 100, 250, 500);
        } else {
            self.tableView.frame = CGRectMake(30, 100, 250, 100);
        }
    } else {
        NSString *str = [NSString stringWithFormat:@"%@",self.arr[indexPath.row]];
        [self.arr replaceObjectAtIndex:indexPath.row withObject:self.arr[0]];
        [self.arr replaceObjectAtIndex:0 withObject:str];
        [self.tableView reloadData];
    }
}

@end

最终效果:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值