「iOS」折叠cell

iOS学习

前言

在暑期仿写中,3G share项目里我们简单的使用了折叠cell。现在写一篇博客来总结该方法。


简单的折叠cell

效果

先看效果:

原理

将cell的高度设置为一个单元格的高度。创建一个按钮,点击按钮时,自定义cell的高度展开,使cell的高度变为展示所有单元格的高度。点击单元格后重新将cell的高度变为单个单元格的高度,也就实现了cell的折叠。下面给出代码实现。

首先将tableView的高度设置为只能容纳一个单元格的高度。

objectivec 复制代码
 self.tableView0 = [[UITableView alloc] initWithFrame:CGRectMake(250, 200, 120, 40) style:UITableViewStylePlain];

添加按钮,并且为其加入响应事件更改tableView的高度。即可实现cell的展开。

objectivec 复制代码
-(void) pressbtn_switch:(UIButton *)btn
{
    btn.selected = !btn.selected;
    if (btn.selected == YES) {
        self.tableView0.frame = CGRectMake(250, 200, 120, 40*4);
    } else {
        self.tableView0.frame = CGRectMake(250, 200, 120, 40);
    }
}

此后,我们使用- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法,即可实现点击单元格,选中后更替折叠cell中的内容。

需要注意的是:我们需要保留原来单元格所展示的内容,只是进行更换。所以此处先获得我们点击的单元格,再用字符串保留单元格的文本内容。

遍历数组,找到我们点击的单元格,更换第一个单元格和点击单元格的内容,最后维护tableView的高度以实现折叠。

objectivec 复制代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    VCUpload_foldCell* cell = [self.tableView0 cellForRowAtIndexPath:indexPath];
    NSString* str = [NSString stringWithString:cell.label.text];
    for (int i = 0; i < 4; i++) {
        if ([str isEqualToString:self.nsarray[i]]) {
            self.nsarray[i] = self.nsarray[0];
            break;
        }
    }
    self.nsarray[0] = str;
    self.btn_switch.selected = !self.btn_switch;
    if (self.btn_switch.selected == YES) {
        self.tableView0.frame = CGRectMake(250, 200, 120, 40*4);
    } else {
        self.tableView0.frame = CGRectMake(250, 200, 120, 40);
    }
    [self.tableView0 reloadData];
}

稍作修改

如果每次更改单元格的内容,都交换数组的位置,稍微显得不人性化。因此可以将单元格的内容多设置一个,将第一个单元格固定展示内容。并且在
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中,加入判断使选中的单元格变色,实现如下效果。

objectivec 复制代码
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = @"VCUpload_foldCell";
    VCUpload_foldCell *cell = [self.tableView0 dequeueReusableCellWithIdentifier:str];
    cell.label.text = self.nsarray[indexPath.row];
    cell.label.backgroundColor = [UIColor whiteColor];
    if (indexPath.row > 0 && [cell.label.text isEqualToString:self.nsarray[0]]) {
        cell.label.backgroundColor = [UIColor colorWithRed:47/255.0f green:134/255.0f blue:196/255.0f alpha:1.0];;
    }
    return cell;
}

总结

对折叠cell的简单原理总结,该UI的设计还是需要对单元格有熟练的基础,才能设计出好看的折叠cell。

相关推荐
Magnetic_h12 小时前
【iOS】锁的原理
笔记·学习·macos·ios·objective-c·cocoa·xcode
Digitally14 小时前
将 iPhone 联系人转移到 Infinix 的完整指南
ios·cocoa·iphone
imLix1 天前
RunLoop 实现原理
前端·ios
归辞...1 天前
「iOS」————设计架构
ios·架构
i紸定i2 天前
解决html-to-image在 ios 上dom里面的图片不显示出来
前端·ios·vue·html·html-to-image
YungFan2 天前
iOS26适配指南之UIButton
ios·swift
红橙Darren2 天前
手写操作系统 - 编译链接与运行
android·ios·客户端
鹏多多.2 天前
flutter-使用device_info_plus获取手机设备信息完整指南
android·前端·flutter·ios·数据分析·前端框架
麦兜*3 天前
【swift】SwiftUI动画卡顿全解:GeometryReader滥用检测与Canvas绘制替代方案
服务器·ios·swiftui·android studio·objective-c·ai编程·swift