iOS实现单元格折叠

折叠的核心是单元格的行数或列数实时变化

比较重要的步骤有:
1.设置数组 (可变数组,用于更新单元格内容)
2.调用方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { (来获取被选中的单元格)
比如我们定义这个tableView 叫做littletableView
NSIndexPath *row = [self.littletableView indexPathForSelectedRow];
上面这个代码用来获取 被选中的单元格的indexPath。
3.如何将选中的单元格内容与第一个单元格内容互换
比如我们现在有一个数组
1 2 3 4 5
我们选中了4
把4用[array insertObject: array [indexPath.row] atIndex:0];这个方法提到1的前面
变成了 4 1 2 3 4 5,此时第二个4的索引加了1
我们在删除时 [array removeObjectAtIndex:indexPath.row+1];索引要加1
4.改变tableView的大小 (这是非常重要的一点,如果你的tableView的frame不够,你即使给他设置10列10行,他也不会改变,因为一旦无法容纳,将不执行定义cell的方法(亲测)

下面是一步一步如何实现

1.在视图控制器的.h文件中,设置俩个UItableView的属性以及一个布尔值来判断单元格是否展开,设置一个可变数组保存单元格的标题信息。
2.在viewDidLoad里将他们初始化设置代理,设置tag值
在实现协议方法时,根据不同的tag值,对应不同方法体 例如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   if (tableView.tag == 101) {
        return 1;
    } else {
        if (cellisOpen == NO) {
            return 1;
        } else {
            return 4;
        }
    }
}

//注意没有设置代理,这些方法将不执行
3.在没有展开时,小单元格的行数为1(我们假设只有一组,行数发生变化),所以

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView.tag == 101) {
        return 1;
    } else {
        return 4;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   if (tableView.tag == 101) {
        return 1;
    } else {
        if (cellisOpen == NO) {
            return 1;
        } else {
            return 4;
        }
    }
}

在创建单元格的函数里,也要区分tag和布尔变量不同的情况的方法体

需要注意的是,在创建时,没有展开的单元格让其一直显示数组的第一个元素

4.接下来是最后一个很关键的函数

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"进入此函数");
    NSIndexPath *row = [self.littletableView indexPathForSelectedRow];
    [arrayrows insertObject:arrayrows[row.row] atIndex:0];   //arrayrows是存储单元格信息的数组
    NSLog(@"%@",arrayrows[row.row]);  //我们来打印一下选中的单元格
    [arrayrows removeObjectAtIndex:row.row+1]; // 删除时选中的单元格索引加1
    for (int i = 0; i < 4; i++) {
        NSLog(@"%@",arrayrows[i]);
    }
    if (tableView.tag == 102) {
        if (cellisOpen == NO) {
        cellisOpen = YES; //更改折叠判断的布尔变量
        } else {
            cellisOpen = NO;//更改折叠判断的布尔变量
        }
    }
    if (cellisOpen == YES) {
        littletableView.frame = CGRectMake(280, 85, 120, 160); // 更改单元格的大小
    } else {
        littletableView.frame = CGRectMake(280, 85, 120, 40);
    }
    NSLog(@"%d",cellisOpen); //打印此时布尔值
    [littletableView reloadData]; // 更新被点击的单元格信息
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 中,要实现点击单元切换视图的功能,有以下几个步骤: 1. 创建一个 UITableView,并设置其代理和数据源。 2. 在代理方法 `tableView(_:didSelectRowAt:)` 中,监听单元的点击事件。 3. 在点击事件中,获取点击的单元索引,然后根据需要进行视图切换。 下面是一个简单的示例代码: ```swift import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let tableView = UITableView() override func viewDidLoad() { super.viewDidLoad() // 设置tableView的frame和代理 tableView.frame = view.bounds tableView.delegate = self tableView.dataSource = self // 注册单元 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") // 添加tableView到视图中 view.addSubview(tableView) } // 返回单元数量 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } // 创建和配置单元 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = "Cell \(indexPath.row + 1)" return cell } // 单元点击事件 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // 获取点击的单元索引 let selectedRow = indexPath.row // 根据需要进行视图切换 switch selectedRow { case 0: // 切换到视图1 let viewController1 = ViewController1() navigationController?.pushViewController(viewController1, animated: true) case 1: // 切换到视图2 let viewController2 = ViewController2() navigationController?.pushViewController(viewController2, animated: true) // 其他切换逻辑... default: break } // 取消选中效果 tableView.deselectRow(at: indexPath, animated: true) } } ``` 在上述示例中,我们创建了一个包含 5 个单元的 UITableView,并通过点击单元来切换到不同的视图。你可以根据自己的需求,定义不同的视图,并在 `didSelectRowAt` 方法中进行切换逻辑的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值