[英]How to find which tableview is editing in textFieldDidEndEditing?
I am using 2 tableviews
我正在使用2个表tableviews
both have textfiled
in tableviewcell
, 两者都在tableviewcell
有textfiled
,
I am getting text of textfield in textFieldDidEndEditing
but how would i know text is being editing belongs to which tableview ,i just want to know the condition where i can differentiate text coming from textfield to store in two different array. 我在textFieldDidEndEditing
获取textfield的文本,但是我怎么知道正在编辑的文本属于哪个tableview,我只想知道我可以区分来自textfield的文本以存储在两个不同数组中的条件。
func textFieldDidEndEditing(_ textField: UITextField)
{
// let indexOf = txtArray.index(of:textField.tag)
if self.tblAnswer.isEditing
{
let indexOf = textField.tag
if let text = textField.text
{
if allAnsText.indices.contains(indexOf)
{
allAnsText.remove(at: indexOf)
}
if text.isEmpty
{
print("Write some text there")
}
else
{
allAnsText.insert(text, at: indexOf)
print(allAnsText)
}
}
}
else if self.tblVideo.isEditing
{
let indexOf = textField.tag
if let text = textField.text
{
if allMediaText.indices.contains(indexOf)
{
allMediaText.remove(at: indexOf)
}
if text.isEmpty
{
print("Write some text there")
}
else
{
allMediaText.insert(text, at: indexOf)
print(allMediaText)
}
}
}
}
Swift 3.x 斯威夫特3.x
Assuming your hierarchy of textFiled is: 假设您的textFiled层次结构为:
TextField->Cell->TableView TextField->单元格-> TableView
Write this line of code in textFieldDidEndEditing
在textFieldDidEndEditing
编写此行代码
print(textField.superview?.superview)
if textField.superview?.superview == myTable1 {
}
else if textField.superview?.superview == myTable2 {
}
else {
}
Forget about traversing the view hierarchy; 忘记遍历视图层次结构; it is error prone and the exact details may change in the future (and break your app horribly). 这容易出错,将来的确切信息可能会更改(并严重破坏您的应用程序)。
Instead, try this: 相反,请尝试以下操作:
Get the text field's location on screen: 获取文本字段在屏幕上的位置:
// Center on text field's own coordinate system let position = textField.frame.center // Center on Window's coordinate system let positionOnWindow = textField.convert(position, to: nil)
Convert that point to the table view's coordinate system, and see if it corresponds to a row: 将该点转换为表格视图的坐标系,然后查看它是否对应于一行:
let positionInTable = tableView1.convert(positionOnWindow, from: nil)</strike> if let indexPath = tableView1.indexPathForRow(at: positionInTable) { // Text field is inside a row of table view 1 } else { // Otherwise }
Note: Code above is untested. 注意:上面的代码未经测试。 May contain compiler errors. 可能包含编译器错误。
Assuming both code snippets run on the same class (your table view delegate, data source and text field delegate) there should be no problem passing the value of positionOnWindow
around (it can all happen inside the text field delegate method). 假设两个代码段都在同一个类(您的表视图委托,数据源和文本字段委托)上运行,则将positionOnWindow
的值传递到周围应该没有问题(所有这些都可以在文本字段委托方法内发生)。
As duly pointed by @rmaddy in the comments, the code above is too roundabout ; 正如@rmaddy在评论中适当指出的那样,上面的代码太 round回; you can directly get the position with: 您可以直接通过以下方式获得职位:
let positionInTable = tableView1.convert(textField.frame.center, from: textField)
(no need the trip to the window's coordinate system and back) (无需前往窗口的坐标系并返回)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.