表格视图内的可展开视图
创始人
2024-12-10 06:01:40
0

要在表格视图内实现可展开视图,可以使用UITableViewController和UITableViewDelegate、UITableViewDataSource协议来实现。

首先,创建一个UITableViewController子类,命名为ExpandableTableViewController。在.h文件中声明一个名为expandedIndexPath的实例变量来记录当前展开的行。

#import 

@interface ExpandableTableViewController : UITableViewController

@property (nonatomic, strong) NSIndexPath *expandedIndexPath;

@end

接下来,在.m文件中实现UITableViewDelegate和UITableViewDataSource协议的方法。

#import "ExpandableTableViewController.h"

@implementation ExpandableTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化expandedIndexPath为nil
    self.expandedIndexPath = nil;
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 返回表格的行数
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    // 设置单元格的标题
    cell.textLabel.text = [NSString stringWithFormat:@"Row %ld", (long)indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 检查当前行是否已展开,如果是,则关闭
    if ([indexPath isEqual:self.expandedIndexPath]) {
        self.expandedIndexPath = nil;
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        return;
    }
    
    // 展开前一个已展开的行
    if (self.expandedIndexPath != nil) {
        NSIndexPath *previousExpandedIndexPath = self.expandedIndexPath;
        self.expandedIndexPath = nil;
        [tableView reloadRowsAtIndexPaths:@[previousExpandedIndexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    
    // 展开当前行
    self.expandedIndexPath = indexPath;
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 如果该行为展开状态,则返回更大的高度,否则返回默认高度
    if ([indexPath isEqual:self.expandedIndexPath]) {
        return 100.0;
    } else {
        return 44.0;
    }
}

@end

在上述代码中,我们通过设置expandedIndexPath来记录当前展开的行的位置。当用户点击某一行时,我们检查该行是否已经展开,如果是,则关闭该行;如果不是,则关闭之前已展开的行并展开当前行。

在展开行的方法中,我们使用UITableView的reloadRowsAtIndexPaths:withRowAnimation:方法来重新加载行。在高度方法中,我们根据行是否展开返回不同的高度。

最后,在其他地方创建一个UITableView实例,并将其代理和数据源设置为ExpandableTableViewController的实例。

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
ExpandableTableViewController *tableViewController = [[ExpandableTableViewController alloc] init];
tableView.delegate = tableViewController;
tableView.dataSource = tableViewController;
[self.view addSubview:tableView];

通过以上代码,你可以实现在表格视图内的可展开视图。

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...