【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
相关推荐
壹方秘境7 小时前
我用Go语言开发了一个跨平台的HTTPS抓包和调试工具
前端·后端·ios
初级代码游戏6 天前
easy Photo Clean公测版:快速清理iPhone照片 邀请公测
ios·iphone
库奇噜啦呼6 天前
【iOS】RunLoop学习
学习·ios
黑科技iOS上架6 天前
iOS应用周末提交什么情况算卡审
经验分享·ios
zzb15806 天前
ios基础-MVC-UIView
ios·mvc·cocoa
kingbal6 天前
Flutter:Flutter SDK版本管理工具FVM
android·flutter·ios·android-studio·window
他们都不看好你,偏偏你最不争气6 天前
【iOS】Runtime - Part 2 && 消息发送:缓存、查找与转发
macos·ios·objective-c·cocoa
2501_915918417 天前
iOS App性能测试工具的实现方法与优化循环指南
android·ios·小程序·https·uni-app·iphone·webview
他们都不看好你,偏偏你最不争气7 天前
【iOS】Runtime - Part 1 && 对象与类的本质
macos·ios·objective-c·cocoa