我需要在 ios 8 中实现可扩展的 tableView 单元格

2023-12-07

In my project I need to implement the UITableview with some of the tableView cells are expandable and some of them are independent. If it is expandable cell need to indicate the '+' symbol.enter image description here. Can any one please help me outenter image description here


我创建了一个小演示,

https://github.com/haripalwagh/ExpandableTableviewCell

Screenshot 1 : Before expanding a cell enter image description here

Screenshot 2 : After expanding a cell enter image description here

@interface ViewController ()
<UITableViewDataSource,
UITableViewDelegate>
{
    UITableView *tblView;

    NSArray *cell0SubMenuItemsArray;

    BOOL isSection0Cell0Expanded;
}

@end

@implementation ViewController

# pragma mark - View Life Cycle

- (void)viewDidLoad
{
    [super viewDidLoad];


    tblView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    tblView.backgroundColor = [UIColor clearColor];
    tblView.delegate = self;
    tblView.dataSource = self;
    tblView.allowsSelection = YES;
    tblView.scrollEnabled = YES;
    tblView.alwaysBounceVertical = YES;
    [self.view addSubview:tblView];

    cell0SubMenuItemsArray = @[@"First Static Menu Item", @"Second Static Menu Item", @"Third Static Menu Item"];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self.view setNeedsLayout];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    [self updateViewDimensions];
}

- (void)updateViewDimensions
{
    tblView.frame = CGRectMake(0, 40, 320, 550);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

# pragma mark - UITableView Delegate and Datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        int cellCount = 2; // Default count - if not a single cell is expanded

        if (isSection0Cell0Expanded)
        {
            cellCount += [cell0SubMenuItemsArray count];
        }

        return cellCount;
    }

    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *strCellId = @"CellId";

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strCellId];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            cell.textLabel.text = @"Expandable Cell";

            UIImageView *accessoryImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

            if (isSection0Cell0Expanded) // Set accessory view according to cell state - EXPANDED / NOT EXPANDED
            {
            accessoryImageView.image = [UIImage imageNamed:@"Minus.png"];
            cell.detailTextLabel.text = @"Status : Expanded";
        }
        else
        {
            accessoryImageView.image = [UIImage imageNamed:@"Plus.png"];
            cell.detailTextLabel.text = @"Status : Not Expanded";
        }

            cell.accessoryView = accessoryImageView;
        }
        else
        {
            if (isSection0Cell0Expanded && [cell0SubMenuItemsArray count] >= indexPath.row) // Check Expanded status and do the necessary changes
            {
                cell.textLabel.text = [NSString stringWithFormat:@"%@", [cell0SubMenuItemsArray objectAtIndex:indexPath.row - 1]];
            }
            else
            {
                cell.textLabel.text = @"Static Cell";
            }
        }
    }

    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        // Change status of a cell reload table

        isSection0Cell0Expanded = !isSection0Cell0Expanded;
        [tblView reloadData];
    }
}

您必须对每个可扩展单元进行这样的管理。 希望对你有帮助..

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我需要在 ios 8 中实现可扩展的 tableView 单元格 的相关文章

随机推荐

  • 升级到 Windows 10 后 WMI 不工作

    我一直在 Windows 8 上的 Visual Studio 中使用以下代码作为控制台应用程序 以返回所连接的串行设备的描述和设备 ID 我在创建的应用程序中使用了该版本的修改版本 以自动检测 Arduino 的 COM 端口 自从我使用
  • Python 中的“线程本地存储”是什么,为什么需要它?

    具体来说 在 Python 中 变量如何在线程之间共享 虽然我用过threading Thread在此之前我从未真正理解或看到变量如何共享的示例 它们是在主线程和子线程之间共享还是仅在子线程之间共享 我什么时候需要使用线程本地存储来避免这种
  • C# - 为 Roman8 字符集创建自己的编码

    C 不支持 Roman8 字符集 但我需要正确使用它 编码 解码字符串 所以我想知道创建自己的编码以支持此编码的最佳方法是什么 够了吗 class Roman8Encoding Encoding 编码器呢 解码器 编码器回退 解码器回退 欢
  • 使用 RTTI 获取 Delphi 中组件的子属性

    我想使用 RTTI 访问以下属性 MyComponent1 Property variable SubProperty 我想要这样的东西 var Ctx TRttiContext Typ TRttiType SubTyp TRttiType
  • 使用 Str_Replace 替换多个单词

    我想用一个特定单词替换多个同义词 结果 本田是一辆汽车 雷克萨斯是一辆汽车 日产是一辆车 赛恩是一辆汽车 有人可以告诉我一种干净的方法 用 汽车 一词替换 车辆 卡车 轿跑车 轿车 而不是我单独替换所有 4 个词
  • 错误:元素应该是“select”,但却是“a”

    当我尝试从下拉列表中选择一个选项时 我收到错误 元素应该是 选择 但却是 a 下面是我的源代码 WebElement element dr findElement By id m1tlm0 Select select new Select
  • 使用函数接口抽象公共代码的问题

    在这里我实现了以下两个功能 如下所示 Output1 computeFirst Input1 input String lockName input getId LockItem lockItem acquireLock lockName
  • 计算已售库存/股票的利润/资本收益

    我有一个 Google 表格 适用 Excel 公式 其中包含 2000 笔交易 其结构如下 链接到 Google Sheet 其中包含示例计算 我需要计算每个卖单的利润 即先进先出资本收益 并将该金额放入第五列 如图所示 这可以通过传统公
  • 为 clion 配置 SFML (Windows)

    我正在我的 Windows 计算机上为学校项目设置工作环境 我们将使用 C 和 CLion 制作一个基本游戏 为了制作游戏 我需要使用 SFML 库 我已经遵循了一些教程 但我似乎无论如何都无法让它工作 I have 下载 CLion 并使
  • 为什么Python请求无法解析TikTok API数据?

    我正在使用请求库向 TikTok 发出 Python 请求 我设法挖掘出他们的用户详细信息的 URL 我不知道这是否合法 如果不合法 请告诉我 当我尝试将其解析为 json 时 它会引发异常 有人可以帮我解析 修复这个问题吗 这是代码 Py
  • 计算一个字母在一个单词中出现的次数

    我有一句话describe 我想看看每个字母在单词中出现了多少次 例如 e 出现两次 d 出现一次等等 我努力了 for letter map str seq describe count re seq letter describe 但我
  • 如何使用更少的 import 语句导入 Java 中的多个类? [复制]

    这个问题在这里已经有答案了 我对在 Java 中使用包还很陌生 我想知道是否有一种更简单的方法来使用更少的 import 语句导入类 我正在使用处理并且我已经开始使用用于处理的 Box2D创建一些游戏 为了使用该库 我必须将以下内容添加到我
  • 为什么这个表达式的计算结果为 0?

    为什么表达 5 lt 3 lt 1 在 MATLAB 中计算结果为 0 每个单独的语句都评估为 true 所以我很困惑为什么它的评估结果为 false 因为它实际上看起来像这样 5 lt 3 lt 1 5 1 最终答案 0
  • 实施 UserDefaults 时遇到问题

    我以前有编码经验 但对 Swift 和 iOS 都非常陌生 我正在开发一款供个人使用的应用程序 以将我用来帮助管理多动症的多个不同应用程序的功能合并到一个地方 该应用程序的主视图提供了一种跟踪日常支出的方法 我正在尝试使用 UserDefa
  • 检查模型与两个不同物体之间是否同时发生碰撞

    在 VR Unity 项目中工作 尝试编写一些 C 脚本 我的角色模型有两只脚 在 VR 中使用跟踪器进行控制 我需要一种方法来找出双脚何时与立方体碰撞 同时 左脚使用立方体 A 右脚使用立方体 B 这样我就可以在满足条件时生成另一个对象
  • 直观地区分自动生成的文件?

    我们有一堆根据数据库模型自动生成的文件 我有过一些捂脸的经历 因为我修改了一些代码 却发现我的修改被覆盖了 因为我没有注意到我的更改在那些生成的文件中 因此 我修改了生成器以在文件头中包含 请勿修改 注释 这有帮助 不过 在跟踪方法调用时
  • 使用 count() 和 first() 时,iPython 笔记本中的 PySpark 会引发 Py4JJavaError

    我在 iPython 笔记本 python v 3 6 中使用 PySpark v 2 1 0 而不是在我的 Mac Sierra 10 12 3 Beta 中使用 virtualenv 1 我通过在终端中拍摄启动了 iPython 笔记本
  • JSON 有效时 JSON 输入意外结束

    我正在使用 Express 从公共 API 获取数据并在前端使用这些数据 这是我的字符路由 它在某些公共 API URL 上运行良好 但我尝试的大多数结果都会出现意外的输入错误结束 我也收到了Unexpected token in JSON
  • 如何使用 Meteor.loginWithGoogle 获取 Google+ 个人资料?

    我正在寻找 Meteor loginWithGoogle 的工作示例 使用meteor 0 6 4 1 我找到了这个用于 loginWithGitHub 的 https www eventedmind com posts meteor cu
  • 我需要在 ios 8 中实现可扩展的 tableView 单元格

    In my project I need to implement the UITableview with some of the tableView cells are expandable and some of them are i