「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。

相关推荐
Digitally1 小时前
如何将文件从 iPhone 传输到 Android(新指南)
android·ios·iphone
YungFan2 小时前
iOS26适配指南之通知
ios·swift
木叶丸2 小时前
跨平台方案该如何选择?
android·前端·ios
我唔知啊3 小时前
OC底层原理二:OC对象的分类(实例对象、类对象、元类对象)
ios·objective-c
泓博4 小时前
KMP(Kotlin Multiplatform)改造(Android/iOS)老项目
android·ios·kotlin
Digitally4 小时前
如何将信息从 iPhone 同步到Mac(完整步骤和示意图)
macos·ios·iphone
大猫会长5 小时前
使用Mac自带的图像捕捉导出 iPhone 相册
ios·iphone
二流小码农10 天前
鸿蒙开发:基于node脚本实现组件化运行
android·ios·harmonyos
依旧风轻10 天前
Domain 层完全指南(面向 iOS 开发者)
ios·domain·entity·sqi
续天续地10 天前
开箱即用的Kotlin Multiplatform 跨平台开发模板:覆盖网络/存储/UI/DI/CI工具链
ios·kotlin