如何在 iOS Swift 项目中访问 UITableViewCell contentView 的子视图中的变量 [英] How to access variables within a subview of UITableViewCell contentView in iOS Swift Project

查看:29
本文介绍了如何在 iOS Swift 项目中访问 UITableViewCell contentView 的子视图中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发带有 UITableView 的 Swift iOS 应用.我使用 XCode Storyboard 向原型单元格添加了一些标签.我还在原型单元格中添加了一个自定义 UIView 类.我可以使用 viewWithTag 访问标签的内容,但我无法访问自定义 UIView 中的变量.我需要传入一些整数,以便正确生成饼图.

I am working on a Swift iOS app with a UITableView. I have used XCode Storyboard to add some labels to a prototype cell. I have also added a custom UIView class to the prototype cell. I can use viewWithTag to access the content of the labels, but I cannot access variables within my custom UIView. I need to pass in some integers so that the pie chart can be properly generated.

我的自定义视图的类是:

The class of my custom view is:

class protoBubbleView: UIView {
var necessaryInteger = 0

我给它分配了一个 9 的标签.我尝试通过这种方式访问​​它:

I have assigned it a tag of 9. I try to access it in this way:

let lbl : protoBubbleView  = cell.viewWithTag(9)!
lbl.necessaryInteger  = 5

但我收到错误,表明 viewWithTag 只返回 UIView 并且这不能分配给 lbl,即 protoBubbleView.

But I receive errors indicating that viewWithTag only returns UIView and this cannot be assigned to lbl which is protoBubbleView.

如何访问此子视图中的变量?

How can I access the variables within this subview?

谢谢.

推荐答案

viewWithTag 返回一个常规的 UIView - 您需要使用 as? 将返回的值转换为 protoBubbleView或 as! 运算符,取决于出现问题时您喜欢的行为(即当没有带有该标签的视图,或者它不是 protoBubbleView 时)有关更多详细信息,请参阅 向下转换

viewWithTag returns a regular UIView - you need to cast the returned value to protoBubbleView using the as? or as! operator, depending on what behaviour you prefer in case of a problem (i.e. when there is no view with that tag, or it is not a protoBubbleView) For more details, see Downcasting

这篇关于如何在 iOS Swift 项目中访问 UITableViewCell contentView 的子视图中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆