[英]UITextField subview of UITableViewCell, get indexPath of cell
[英]Get indexPath of UITextField in UITableViewCell with Swift
因此,我正在构建一个详细信息视图控制器应用程序,该应用程序显示一个包含两部分单元格的表格:标签和文本字段。
我正在尝试检索文本字段值并将其添加到数组中。 我尝试使用“ textField.superview.superview
”技术,但没有奏效。
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
”。 引用表有两部分,分别为四行和两行。
提前致谢。
编辑:在函数的第二行添加了“.superview”。
虽然当前接受的答案可能有效,但它假定了特定的视图层次结构,这不是一种可靠的方法,因为它很容易发生变化。
要从单元格内的UITextField
获取indexPath
,最好使用以下方法:
func textFieldDidEndEditing(textField: UITextField!){
let pointInTable = textField.convert(textField.bounds.origin, to: self.tableView)
let textFieldIndexPath = self.tableView.indexPathForRow(at: pointInTable)
...
}
这将独立于视图层次结构的最终更改而继续工作。
您需要在函数中投射第一行和第二行,如下所示:
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,因此您需要将其转换为您期望的视图类型。
使用超级视图和类型转换不是首选的方法。 最佳实践是使用委托模式。 如果您在 DemoTableViewController 中使用的 DemoTableViewCell 中有一个 textField,请创建一个协议 DemoTableViewCellDelegate 并将 DemoTableViewCell 的委托分配给 DemoTableViewController,以便在文本字段中结束时通知视图控制器。
protocol DemoTableViewCellDelegate: class {
func didEndEditing(onCell cell: DemoTableViewCell)
}
class DemoTableViewCell: UITableViewCell {
@IBOutlet var textField: UITextField!
weak var delegate: DemoTableViewCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
textField.delegate = self
}
}
extension DemoTableViewCell: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
delegate.didEndEditing(onCell: self)
}
}
class DemoTableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: DemoTableViewCell.self, for: indexPath)
cell.delegate = self
return cell
}
}
extension DemoTableViewController: DemoTableViewCellDelegate {
func didEndEditing(onCell cell: DemoTableViewCell) {
//Indexpath for the cell in which editing have ended.
//Now do whatever you want to do with the text and indexpath.
let indexPath = tableView.indexPath(for: cell)
let text = cell.textField.text
}
}
您可以使用UITableViewCell
tag
属性
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UpdateTableViewCell", for: indexPath) as! UpdateTableViewCell
cell.tag = indexPath.row
cell.setCellData()
return cell
}
现在在UITableViewCell
func textFieldDidEndEditing(textField: UITextField!){
let textFieldIndexPath = self.tag
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.