使用Swift在UITableViewCell中获取UITextField的indexPath [英] Get indexPath of UITextField in UITableViewCell with Swift

查看:94
本文介绍了使用Swift在UITableViewCell中获取UITextField的indexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在构建一个Detail View Controller应用程序,该应用程序呈现一个包含两部分单元格的表格:标签和文本字段.

So, I'm building a Detail View Controller App that presents a Table with a two-part cell: the label and the Text Field.

我正在尝试检索文本字段"值并将其添加到数组中. 我尝试使用"textField.superview.superview"技术,但是没有用.

I'm trying to retrieve the Text Field value and add it to an array. I tried to use the "textField.superview.superview" technique but it didn't worked.

func textFieldDidEndEditing(textField: UITextField!){
    var cell: UITableViewCell = textField.superview.superview
    var table: UITableView = cell.superview.superview
    let textFieldIndexPath = table.indexPathForCell(cell)
}

Xcode无法生成,并显示"UIView不可转换为UITableViewCell"和至UITableView". 引用表分为两部分,分别为四行和两行.

Xcode fails to build and presents that "UIView is not convertible to UITableViewCell" and "to UITableView". The referring table has two sections, of four and two rows, respectively.

谢谢.

在函数的第二行添加了".superview".

added ".superview" at the second line of the function.

推荐答案

您将希望在函数中强制转换第一行和第二行,如下所示:

You'll want to cast the first and second lines in your function, like this:

func textFieldDidEndEditing(textField: UITextField!){
    var cell: UITableViewCell = textField.superview.superview as UITableViewCell
    var table: UITableView = cell.superview as UITableView
    let textFieldIndexPath = table.indexPathForCell(cell)
}

superview返回一个UIView,因此您需要将其强制转换为所需的视图类型.

superview returns a UIView, so you need to cast it to the type of view you expect.

这篇关于使用Swift在UITableViewCell中获取UITextField的indexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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