简体   繁体   English

如何将子视图添加到TableViewCell?

[英]How to add a subview to a TableViewCell?

I need to add a subview to TableViewCell when the button in TableViewCell is clicked it should show another tableview as a subview and height of the tableview should be dynamic according to number of cell. 单击TableViewCell中的按钮时,我需要向TableViewCell添加一个子视图,它应该显示另一个表视图作为子视图,并且该表视图的高度应根据单元格的数量动态变化。

How can I do this? 我怎样才能做到这一点?

我怎样才能做到这一点

Above screenshot is taken from a very popular shopping app and I need to do the same in my project. 上面的屏幕截图来自一个非常受欢迎的购物应用程序,我需要在我的项目中做同样的事情。

You can easily achieve this using simple UITableView . 您可以使用简单的UITableView轻松实现此目的。 For that you need to set all your main categories as your UITableview section and respected sub-categories can be added to respective rowOfSection . 为此,您需要将所有主要类别设置为UITableview部分,并且可以将受尊重的子类别添加到相应的rowOfSection中

您可以仅使用一个具有UITableViewStyleGrouped样式的表视图,并将“男人”,“妇女”,“儿童和婴儿”设置为TableView部分标题,保留一个布尔值以确定每个部分的“ numberOfRowsInSection:”结果并重新加载tableview 。

See the below link at github: 参见下面的链接在github:

https://github.com/OliverLetterer/SLExpandableTableView https://github.com/OliverLetterer/SLExpandableTableView

This contains the Expandable TableView, which you required. 这包含您需要的Expandable TableView。 You have to implement SLExpandableTableViewDelegate and SLExpandableTableViewDatasource which contains different method, in which you have to provide inner tableview as well. 您必须实现SLExpandableTableViewDelegateSLExpandableTableViewDatasource ,它们包含不同的方法,其中还必须提供内部tableview。

Hope this helps you. 希望这对您有所帮助。

You need to do some work on your own I can give the directions that'll give you a way from my point of view :- 您需要自己做一些工作,我可以提供一些指示,从我的角度来看,这将为您提供一种方法:-

  1. Make a Custom cell which you want to expand. 制作要扩展的自定义单元格。
  2. While designing the cell make its height in storyboard like 200 or so according to your need and add all the elements those you want to see when the cell is expanded. 在设计单元格时,根据需要将其在情节提要板上的高度设为200左右,并添加所有在单元格展开时要查看的元素。

  3. You'll need two delegate methods first -didSelectItemAtIndexPath and second HeightForRowAtIndexPath at index path. 您将在索引路径处需要两个委托方法,第一个-didSelectItemAtIndexPath和第二个HeightForRowAtIndexPath。

  4. First You need to make sure the user taps on the button or cell that you want to expand , and to achieve that you need to call didSelectItemAtIndexPath. 首先,您需要确保用户点击了要扩展的按钮或单元,并实现了您需要调用didSelectItemAtIndexPath的功能。

  5. Once you get your cell Position, in HeightForRowAtIndexPath check the indexpath is equal to your cell's indexpath,if yes then return the exact height(ie:200) of your cell otherwise return the default(ie:70) height of you cell. 一旦获得单元格位置,在HeightForRowAtIndexPath中检查indexpath等于单元格的indexpath,如果是,则返回单元格的确切高度(即:200),否则返回单元格的默认高度(即:70)。

Note : In didSelectItemAtIndexPath you need to call to method to update the current cell 注意 :在didSelectItemAtIndexPath中,您需要调用方法来更新当前单元格

[self.tableView beginUpdates];
[self.tableView endUpdates];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM