简体   繁体   English

Swift中视图的关联值

[英]Associated Values of a view in Swift

I have a UILabel and need to reference its initial indexPath.row in another view controller? 我有一个UILabel ,需要在另一个视图控制器中引用其初始indexPath.row吗? I have already used the tag of the UILabel for a different purpose, but is there a way I can set an associated value of some sort with the UILabel in swift. 我已经将UILabel的标签用于其他目的,但是有一种方法可以快速设置UILabel的某种关联值。

Your UILabel should not be acting as a store for data, that's what data structures are for. 您的UILabel不应充当数据存储,这就是数据结构的目的。 One simple possibility to start out with is to have the tag be the index of that label's data in an array or use the tag value as the key of a dictionary and store the data you need in those data structures. 首先,一种简单的可能性是使标签成为该标签数据在数组中的索引,或者将标签值用作字典的键,然后将所需的数据存储在这些数据结构中。

If you are using custom view for your TableViewCell (which I assume you are ), I would just create new custom view (inherited from UILabel) and add another property to it for indexPath and assign this property with the original indexPath, I would use this custom label view instead of UILabel. 如果您正在为TableViewCell使用自定义视图(我假设您是),那么我将创建一个新的自定义视图(继承自UILabel),并为其添加另一个属性作为indexPath,并使用原始indexPath分配此属性,自定义标签视图,而不是UILabel。 Please let me know if you have questions about this approach. 如果您对此方法有疑问,请告诉我。

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

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