【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
相关推荐
2501_915918412 小时前
Fiddler中文版全面评测:功能亮点、使用场景与中文网资源整合指南
android·ios·小程序·https·uni-app·iphone·webview
不知名It水手4 小时前
uniapp运行项目到ios基座
ios·uni-app·cocoa
Digitally5 小时前
[5种方法] 如何将iPhone短信保存到电脑
ios·iphone
杂雾无尘8 小时前
Swift 5.9 新特性揭秘:非复制类型的安全与高效
ios·swift·apple
Thomas_YXQ11 小时前
Unity3D iOS闪退问题解决方案
ios
Daniel_Coder14 小时前
iOS Widget 开发-7:TimelineProvider 机制全解析:构建未来时间线
ios·swift·widget
Daniel_Coder15 小时前
iOS Widget 开发-3:Widget 的种类与尺寸(主屏、锁屏、灵动岛)
ios·swift·widget
Engandend1 天前
Flutter与iOS混合开发交互
flutter·ios·程序员
山水域1 天前
GoogleAdsOnDeviceConversion 库的作用与用法
ios
Lucifer晓1 天前
记录一次Flutter项目上传App Store Connect出现“Validation failed”错误的问题
flutter·ios