简体   繁体   English

如何动态更改Tableview的自定义单元格宽度?

[英]How to change Tableview's custom cell width dynamically ?

I have a table view and its every cell`s width say 1024px , so my requirement is after clicking a cell tableview's width will change logically. 我有一个表格视图,它的每个单元格的宽度都说1024px,所以我的要求是单击一个单元格表格视图的宽度后,逻辑上会改变。 And my table view is bit complex. 而且我的表格视图有点复杂。 Every cell contains a custom view which is defined in another class. 每个单元格都包含一个在另一个类中定义的自定义视图。 Please help me.. 请帮我..

I do not believe there is a way to modify a single cell's width without changing the width of the table view, only height using tableView:heightForRowAtIndexPath: . 我不相信有一种方法可以在不更改表格视图宽度的情况下修改单个单元格的宽度,而仅使用tableView:heightForRowAtIndexPath:来更改高度。

Therefore, you might want to change the width of the custom view inside your cell. 因此,您可能需要更改单元格内自定义视图的宽度。

First, to get the cell in question, call cellForRowAtIndexPath: on your table view wherever you need (if you want to change the width on tap, that would probably be in your tableView:didSelectRowAtIndexPath: ). 首先,要获取有问题的单元格,请在需要的地方在表视图上调用cellForRowAtIndexPath:如果要更改水龙头的宽度,则可能在tableView:didSelectRowAtIndexPath: )。

To access your custom view, you may give your custom view a tag number inside of the UITableViewCell by setting the custom view's tag property when creating the cell or in the storyboard. 要访问您的自定义视图,可以在创建单元格或在情节提要中通过设置自定义视图的tag属性,在UITableViewCell内为自定义视图提供一个标记号。 Then, call viewWithTag: on the UITableViewCell instance to get the custom view, and modify the width of its frame property. 然后,在UITableViewCell实例上调用viewWithTag:以获取自定义视图,并修改其frame属性的width

Another, perhaps more suitable option would be to use a custom UITableViewCell class for your table view cells. 另一个也许更合适的选择是为表视图单元格使用自定义UITableViewCell类。 That would mean subclassing UITableViewCell and creating a property for your custom view, which would allow you to access the custom subview through the getter and then change its frame property. 这将意味着子类化UITableViewCell并为您的自定义视图创建一个属性,这将允许您通过getter访问自定义子视图,然后更改其frame属性。

If you need the width of the custom view's container to change (which is currently your cell), simply embed the custom view inside a UIView and modify the UIView's width rather than the UITableViewCell 's instance in the same manner described above. 如果您需要更改自定义视图容器的宽度(当前是您的单元格),则只需将自定义视图嵌入UIView然后以与上述相同的方式修改UIView的宽度,而不是UITableViewCell的实例。

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

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