【iOS】折叠cell

折叠cell

前言

暑假在仿写3G share时使用到了折叠cell,故而现在来总结一下折叠cell,如有不足,还望指正。

实现原理

在OC中,scrollview是tableview的父类,要想实现折叠cell的效果,我们需要控制在点击按钮前后,tableview的高度变换,同时在点击cell时,将点击的cell放在数组的第一个元素,同时收回展示的cell即可。这样我们就可以实现cell的切换。

代码实现

这里先展示实现的效果:

原理讲解

折叠cell其实就是通过按钮控制tableview的大小,在每次点击显示的单独的一个cell的时候,将整个tableview显示出来,在点击非第一个cell的时候,将数组的顺序重新排列,让点击的cell排在第一位,同时缩小tableview的大小,并且刷新tableview,这样做,就可以实现折叠cell的效果了。

代码展示

objectivec 复制代码
- (void)viewDidLoad {
    [super viewDidLoad];
    self.arr = [NSMutableArray arrayWithObjects:@"111", @"222", @"333", @"444", nil];
    _tableview = [[UITableView alloc] init];
    self.tableview.frame = CGRectMake(200, 300, 100, 30);
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.frame = CGRectMake(170, 300, 30, 30);
    self.btn.tag = 102;
    [self.btn addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
    [self.btn setImage:[UIImage imageNamed:@"未勾选_副本.png"] forState:UIControlStateNormal];
    [self.btn setImage:[UIImage imageNamed:@"已勾选_副本.png"] forState:UIControlStateSelected];//按钮的两个状态
    self.btn.selected = NO;
    [self.view addSubview:self.btn];
    [self.view addSubview:self.tableview];
}

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

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"id"];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"id"];
    }
    cell.textLabel.text = _arr[indexPath.row];
    cell.textLabel.textColor = [UIColor blackColor];
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* str = _arr[indexPath.row];
    [_arr removeObjectAtIndex:indexPath.row];
    [_arr insertObject:str atIndex:0];
    [self.tableview reloadData];
    [self press];
}

-(void) press
{
    if(self.btn.tag == 101) {
        self.tableview.frame = CGRectMake(200, 300, 100, 30);
        self.btn.tag++;
        self.btn.selected = NO;
    } else {
        self.tableview.frame = CGRectMake(200, 300, 100, 150);
        self.btn.tag--;
        self.btn.selected = YES;
    }
}//控制是否更改tableview的范围

@end
相关推荐
吴Wu涛涛涛涛涛Tao7 小时前
深入理解 Swift Codable:从基础到进阶
ios
Jouzzy13 小时前
【iOS安全】iPhone X iOS 16.7.11 (20H360) WinRa1n 越狱教程
安全·ios·iphone
roman_日积跬步-终至千里1 天前
【Go语言基础】基本语法
开发语言·golang·xcode
二流小码农1 天前
鸿蒙开发:实现一个标题栏吸顶
android·ios·harmonyos
season_zhu1 天前
iOS开发:关于日志框架
ios·架构·swift
Digitally1 天前
如何在电脑上轻松访问 iPhone 文件
ios·电脑·iphone
安和昂1 天前
【iOS】YYModel源码解析
ios
pop_xiaoli1 天前
UI学习—cell的复用和自定义cell
学习·ui·ios
Daniel_Coder1 天前
Xcode 16.4 + iOS 18 系统运行时崩溃:___cxa_current_primary_exception 符号丢失的原因与解决方案
ios·xcode·ios 18·dyld·libc++abi
烈焰晴天1 天前
使用ReactNative加载Svga动画支持三端【Android/IOS/Harmony】
android·react native·ios