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

相关推荐
若水无华1 天前
fiddler 配置ios手机代理调试
ios·智能手机·fiddler
Aress"1 天前
【ios越狱包安装失败?uniapp导出ipa文件如何安装到苹果手机】苹果IOS直接安装IPA文件
ios·uni-app·ipa安装
Jouzzy1 天前
【iOS安全】Dopamine越狱 iPhone X iOS 16.6 (20G75) | 解决Jailbreak failed with error
安全·ios·iphone
瓜子三百克1 天前
采用sherpa-onnx 实现 ios语音唤起的调研
macos·ios·cocoa
左钦杨1 天前
IOS CSS3 right transformX 动画卡顿 回弹
前端·ios·css3
努力成为包租婆2 天前
SDK does not contain ‘libarclite‘ at the path
ios
安和昂2 天前
【iOS】Tagged Pointer
macos·ios·cocoa
I烟雨云渊T3 天前
iOS 阅后即焚功能的实现
macos·ios·cocoa
struggle20253 天前
适用于 iOS 的 开源Ultralytics YOLO:应用程序和 Swift 软件包,用于在您自己的 iOS 应用程序中运行 YOLO
yolo·ios·开源·app·swift
Unlimitedz3 天前
iOS视频编码详细步骤(视频编码器,基于 VideoToolbox,支持硬件编码 H264/H265)
ios·音视频