【问题标题】:How to put a tableview inside one of the cell of another tableView? any idea? [duplicate]如何将 tableview 放在另一个 tableView 的一个单元格中?任何想法? [复制]
【发布时间】:2014-03-21 02:18:05
【问题描述】:

在 tableView 的一个单元格中,我想放置另一个 tableView(例如 toyTable)。任何人都可以帮助我吗?我在我的项目中创建了一个 tableView(主要)和另一个 tableview。现在在我的主 tableView 中,我有很多单元格,在其中一个单元格中,我希望另一个表(toyTable)出现在单元格中。

【问题讨论】:

  • 为什么你需要在另一个表里面。任何特殊原因
  • 你想要达到的目标
  • @santhu:你能告诉我那个输出的图片吗?那个答案在你发布的链接中打勾是正确的吗?请..我非常需要它。

标签: ios objective-c ipad uitableview


【解决方案1】:

将表格视图放在表格单元格中是一个糟糕的设计。将toyTable 插入为主表视图的部分。

【讨论】:

  • 为什么将第二个表格视图添加为部分而不是作为内容视图的子项更好?您是否正在考虑其他功能,例如部分折叠?
  • @Jano 我猜,重用内部表格视图的单元格会有问题。折叠可能是表数据源的职责。
【解决方案2】:

你为什么要这样做,因为嵌套 tableview 而不是 UITableview 已经具有展开和折叠属性是一个坏主意。 使用下面的链接作为参考http://www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/

【讨论】:

    【解决方案3】:

    在 cellForRowAtIndexPath 中添加该表并执行如下代码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
       if (tableView == mainTable) {
         //do this for main table
       }
       else { 
         //do this for second table
       }
    
    }
    

    【讨论】:

      【解决方案4】:

      假设你有两张桌子。

      为它们创建属性

      // This one might be connected as an outlet with your storyboard
      @property (nonatomic) IBOutlet UITableViewController *mainTable;
      // This one should be in the .m file
      @property (nonatomic) UITableViewController *toyTable;
      

      为每个实现委托和数据源协议方法。

      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
      {
          if (tableView == self.mainTable) {
              return 2;
          } else {
              return 1;
          }
      }
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
          if (tableView == self.mainTable) {
              return 10;
          } else {
              return 5;
          }
      }
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          if (tableView == self.mainTable) {
              // If this is the indexPath for the cell that should contain 
              // the nested table view, initialize your self.toyTable, set 
              // datasource and delegate etc.
              // Else configure your default outer cell
          } else {
              // Configure the cells of your self.toyTable;
          }
      }
      
      - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          if (tableView == self.mainTable) {
              // If this is any normal outer cell
              return UITableViewAutomaticDimension;
              // If this is the outer cell that contains self.toyTable
              return some bigger value (if desired)
          } else {
              // The height of the inner cell
              return UITableViewAutomaticDimension;
          }
      }
      
      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
      {
          if (tableView == self.mainTable) {
              // If this is the outer cell that contains self.toyTable
              // You should ignore the selection
              // Else handle it according to your needs
          } else {
              // Handle click on inner cell
          }
      }
      

      如果您使用静态表格视图作为 mainTable,您可以直接将表格视图拖到您的一个静态单元格上并为其定义原型单元格等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多