如何找到在textFieldDidEndEditing中正在编辑的表视图? [英] How to find which tableview is editing in textFieldDidEndEditing?

查看:80
本文介绍了如何找到在textFieldDidEndEditing中正在编辑的表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用2 tableviews

tableviewcell中都具有textfiled

我在textFieldDidEndEditing中获取文本字段的文本,但是我怎么知道正在编辑的文本属于哪个表视图,我只想知道可以区分文本字段中的文本并将其存储在两个不同数组中的条件. /p>

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.

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)
            }
        }

    }

}

推荐答案

忘记遍历视图层次结构;这容易出错,将来的确切信息可能会更改(并严重破坏您的应用程序).

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:

  1. 获取文本字段在屏幕上的位置:

  1. 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.

    假设两个代码段都在同一个类上运行(您的表视图委托,数据源和文本字段委托),则将positionOnWindow的值传递到周围应该没有问题(它们都可以在文本字段委托方法内发生)

    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).

    正如@rmaddy在评论中指出的那样,上面的代码是环形交叉路口;您可以直接通过以下方式获取职位:

    As duly pointed by @rmaddy in the comments, the code above is too roundabout; you can directly get the position with:

    let positionInTable = tableView1.convert(textField.frame.center, from: textField)
    

    (无需往返于窗口的坐标系并返回)

    这篇关于如何找到在textFieldDidEndEditing中正在编辑的表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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