
func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell { var cell : UItableVIEwCell? = nil let objectAtIndexPath: AnyObject = contentArray![indexPath.row] if let questionText = objectAtIndexPath as? String { cell = tableVIEw.dequeueReusableCellWithIDentifIEr("questionCell",forIndexPath: indexPath) as QuestiontableVIEwCell cell.customLabel.text = "test" } return cell! } 在这里我得到了错误
UItableVIEwCell没有属性customLabel
QuestiontableVIEwCell有哪些.我的演员到QuestiontableVIEwCell有什么问题?
解决方法 问题不是你的演员,而是你的细胞宣言.您将其声明为可选的UItableVIEwCell,并且该声明永远保留 – 并且是编译器所知道的全部内容.因此,您必须在调用customLabel时进行强制转换.而不是这个:
cell.customLabel.text = "test"
你需要这个:
(cell as QuestiontableVIEwCell).customLabel.text = "test"
你可以通过声明一个不同的变量来让自己变得更容易(因为你知道在这种特殊情况下你的单元格将是一个QuestiontableVIEwCell),但只要你只有一个变量,单元格,你将不得不经常演员无论你认为它真的会是什么课程.就个人而言,我会写一些更像这样的东西,完全是为了避免重复投射:
if let questionText = objectAtIndexPath as? String { let qtv = tableVIEw.dequeueReusableCellWithIDentifIEr("questionCell",forIndexPath: indexPath) as QuestiontableVIEwCell qtv.customLabel.text = "test" cell = qtv } 总结 以上是内存溢出为你收集整理的ios – 如何在Swift中正确转换为子类?全部内容,希望文章能够帮你解决ios – 如何在Swift中正确转换为子类?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)