iphone - 每当我们需要单击一个单元格时如何显示表格 View 单元格

标签 iphone objective-c ios ipad

我正在该应用程序中编写一个应用程序,我必须显示带有两个单元格的表格 View 。而用户触摸的第一个单元格则必须显示 View 中的一些其他单元格(我不能在此处使用导航)以及第二个单元格也可见..任何人都可以告诉我对此的建议吗? 谢谢大家。

最佳答案

如果您想在单击单元格时插入表格 View 单元格,请尝试以下代码。

假设您要在第 0 部分的位置 1、2 和 3 处插入 3 行。在 didSelectRowAtIndexPath 方法中,执行以下操作。如果我们在第 0 节(第一节)中选择第 0 行(第一行),则行将被插入到第 0 节中。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

   if (indexPath.row == 0 && indexPath.section == 0) {

       // Create indexPaths for the rows you are going to insert

       NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0]];
       NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:2 inSection:0]];
       NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:3 inSection:0]];

       // Increase the number of rows in section 0 by 3, as we are inserting 3 rows here

       numberOfRowsInSectionZero = numberOfRowsInSectionZero + 3;

       // Insert the rows

       [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath1, indexPath2, indexPath3, nil] withRowAnimation:UITableViewRowAnimationFade];
    }
}

在您的 numberOfRowsInSection 方法中,您应该具有如下所示的内容,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {

    // some code here

    if (section == 0) return numberOfRowsInSectionZero;

    // some code here
}

并确保您在cellForRowAtIndexPath方法中有适当的代码来显示新显示的行中的内容。

注意:您必须更改代码,以便在单击第 0 节的第 0 行时不会插入行,而这些行已经被插入。

关于iphone - 每当我们需要单击一个单元格时如何显示表格 View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4498992/

相关文章:

objective-c - 如何有效地获取 NSDictionary 中键值的反向映射?

ios - App Store是否允许用户下载设备OS不支持的更新?

iphone - slider /文本框交换数据

objective-c - 如何将选项卡 View 项连接到操作? (OS X 上的 cocoa )

ios - 如何使带有 AVCaptureVideoPreviewLayer 的 UIView 看起来更好?

ios - 在 Swift 2 中过滤任何对象

ios - 呈现适用于 iOS7 和 iOS8 的半透明 View Controller

iphone - 如何在 TableView 中创建带有标签的工具栏?

ios - 如何在 iPhone 应用程序中创建弹出窗口?

javascript - 如何在 UIWebView 上运行 HTML+javascript 文件。 .?