1

I am using 2 tableviews

both have textfiled in tableviewcell,

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

    }

}

2 Answers2

2

Forget about traversing the view hierarchy; it is error prone and the exact details may change in the future (and break your app horribly).

Original Answer (for posterity)

Instead, try this:

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

Better Answer

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)

(no need the trip to the window's coordinate system and back)

Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189
0

Swift 3.x

Assuming your hierarchy of textFiled is:

TextField->Cell->TableView

Write this line of code in textFieldDidEndEditing

print(textField.superview?.superview)
if textField.superview?.superview == myTable1 {

}
else if textField.superview?.superview == myTable2 {

}
else {

}
Salman Ghumsani
  • 3,647
  • 2
  • 21
  • 34
  • Why don't you use `else if` or directly `else` instead, Here two if is meaningless – Prashant Tukadiya Sep 22 '17 at 07:31
  • Isn't there the cell's **content view** in between the text field and the cell? Internal structure of `UITableView` and `UITableviewCell` are implementation details, not part of the public API. Code that relies on that structure is very fragile. – Nicolas Miari Sep 22 '17 at 07:53
  • This is not the correct way. This makes bad assumptions about the private layout of a cell class and the table view class. – rmaddy Dec 16 '18 at 22:40