折叠单元格的使用

原理

折叠单元格是在原来单元格上的扩展,实际上可有UItableview和UIlabel以及一个储存数据的可变数组组成,以及控制折叠的按钮;通过按钮可以控制Uitableview的布局以及单元格的数量来实现折叠;

实现图片

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码实现和注意事项

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic) UIButton* btn ;
@property (nonatomic) UITableView* tableview ;
@property (nonatomic) UILabel* label ;
@property (nonatomic) NSMutableArray* arraydata ;

@end

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITableView* tableview01 = [[UITableView alloc] initWithFrame:CGRectMake(30, 100, 100, 50)  style:UITableViewStyleGrouped] ;
    [self.view addSubview:tableview01] ;
    self.tableview = tableview01 ;
    //
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom] ;
    self.btn.frame = CGRectMake(130, 110, 30, 30) ;
    [self.btn setImage:[UIImage imageNamed:@"xiajiantou.png"] forState:UIControlStateNormal] ;
    [self.btn addTarget:self action:@selector(presstap) forControlEvents:UIControlEventTouchUpInside] ;
    [self.view addSubview:self.btn] ;
    [self.btn setSelected:NO] ;
    //
    self.tableview.delegate = self ;
    self.tableview.dataSource = self ;
    //
    self.arraydata = [NSMutableArray arrayWithObjects:[NSString stringWithFormat:@"1"],[NSString stringWithFormat:@"2"],[NSString stringWithFormat:@"3"], [NSString stringWithFormat:@"4"] , [NSString stringWithFormat:@"5"], [NSString stringWithFormat:@"6"], [NSString stringWithFormat:@"7"], [NSString stringWithFormat:@"8"], nil] ;
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(30, 100, 100, 50)] ;
    [self.view addSubview:self.label] ;
    self.label.text = self.arraydata[0] ;
    self.label.textAlignment = NSTextAlignmentCenter ;
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.btn.selected == NO) {
        return 0;
    } else {
        return 8;
    }
}

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

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cell = [self.tableview dequeueReusableCellWithIdentifier:@"1"] ;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"1"] ;
    }
    for (int i = 0; i < 8;i++) {
        if (indexPath.row == i) {
            cell.textLabel.text = self.arraydata[i] ;
        }
    }
    return cell;
}

- (void)presstap {
    if (self.btn.selected == NO) {
        self.btn.selected = YES ;
        self.tableview.frame = CGRectMake(30, 100, 100, 400) ;
    } else {
        self.btn.selected = NO ;
        self.tableview.frame = CGRectMake(30, 100, 100, 50) ;
    }
    [self.tableview reloadData] ;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString* str = self.arraydata[indexPath.row] ;
    self.arraydata[indexPath.row]  = self.arraydata[0];
    self.arraydata[0] = str ;
    self.label.text = self.arraydata[0] ;
    [self.tableview reloadData] ;
    [self presstap] ;
    
}


@end

注意单元格显示的地方是一个label;
以及单元格更新的时间,要同时更新cell和label ;
以及存储数据的数组内元素的交换;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值