折叠cell

折叠cell

在暑假任务3Gshare中用到了折叠cell;
具体效果如下:在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
首先设定两个属性为:tableView和存储cell的元素数据的可变数组。

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview1;
@property (nonatomic, strong) NSMutableArray *array;

@end

然后对两个属性进行初始化,注意对tableView1的tag只进行设定,从而通过tag的值来判断cell的展开还是收缩状态。

self.tableview1 = [[UITableView alloc] initWithFrame:CGRectMake(100, 100, 430 - 200, 100)];
    self.tableview1.delegate = self;
    self.tableview1.dataSource = self;
    self.tableview1.tag = 0;
    [self.tableview1 registerClass:[UITableViewCell class] forCellReuseIdentifier:@"1"];
    [self.view addSubview:_tableview1];
    
    _array = [NSMutableArray arrayWithObjects:@"原创作品",@"设计教程",@"设计资料",@"设计师观点", nil];

然后要完成tableView的协议函数:

//获取每组单元格的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_tableview1.tag == 0) {
        return 1;
    } else {
        return 4;
    }
}

//获取单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 20;
}

然后往cell中填充不同的信息:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableview1 dequeueReusableCellWithIdentifier:@"1" forIndexPath:indexPath];
    cell.textLabel.text = _array[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

然后写点击cell以及数据交换的相关方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_tableview1.tag == 0) {
        _tableview1.frame = CGRectMake(100, 100, 430 - 200, 400);
        _tableview1.tag = 1;
        [_tableview1 reloadData];
    } else {
        UITableViewCell *cell0 = [self.tableview1 cellForRowAtIndexPath:indexPath];
        NSString *str = [NSString stringWithFormat:@"%@", cell0.textLabel.text];
        
        for (int i = 0; i < 4; i++) {
            if ([_array[i] isEqualToString:str]) {
                _array[i] = _array[0];
                break;
            }
        }
        _array[0] = str;
        _tableview1.frame = CGRectMake(100, 100, 430 - 200, 100);
        _tableview1.tag = 0;
        [_tableview1 reloadData];
    }
}

全部代码如下:

//ViewController.h文件
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview1;
@property (nonatomic, strong) NSMutableArray *array;

@end

//ViewController.m文件
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableview1 = [[UITableView alloc] initWithFrame:CGRectMake(100, 100, 430 - 200, 100)];
    self.tableview1.delegate = self;
    self.tableview1.dataSource = self;
    self.tableview1.tag = 0;
    [self.tableview1 registerClass:[UITableViewCell class] forCellReuseIdentifier:@"1"];
    [self.view addSubview:_tableview1];
    
    _array = [NSMutableArray arrayWithObjects:@"原创作品",@"设计教程",@"设计资料",@"设计师观点", nil];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableview1 dequeueReusableCellWithIdentifier:@"1" forIndexPath:indexPath];
    cell.textLabel.text = _array[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

//获取每组单元格的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_tableview1.tag == 0) {
        return 1;
    } else {
        return 4;
    }
}

//获取单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_tableview1.tag == 0) {
        _tableview1.frame = CGRectMake(100, 100, 430 - 200, 400);
        _tableview1.tag = 1;
        [_tableview1 reloadData];
    } else {
        UITableViewCell *cell0 = [self.tableview1 cellForRowAtIndexPath:indexPath];
        NSString *str = [NSString stringWithFormat:@"%@", cell0.textLabel.text];
        
        for (int i = 0; i < 4; i++) {
            if ([_array[i] isEqualToString:str]) {
                _array[i] = _array[0];
                break;
            }
        }
        _array[0] = str;
        _tableview1.frame = CGRectMake(100, 100, 430 - 200, 100);
        _tableview1.tag = 0;
        [_tableview1 reloadData];
    }
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值