1

I have the structure of my TableViewController as such (mentioning for delegation purposes):

----View
-----Scroll View
------Content View
-------TableView
--------TableViewCell

I can get all of the rows and sections of my TableView to load correctly. Each TableViewCell has a UITextField which is editable. When the user begins to edit the UITextField, I need to be able to retrieve the contents of what the user types. To update the correct data sets, the way I am currently doing it is retrieving the indexPath of the UITextField

My issue is this: When I scroll slowly and click on the UITextField one by one, I do not crash. However, if I click on a cell and scroll down quickly (even if I stay in the same section), I crash due to the indexPath being null.

In my delegate I have this:

func textFieldDidEndEditing(_ textField: UITextField) {
    //here is the code I am using to determine the indexPath
    let view = textField.superview!
    let cell = view.superview as! UITableViewCell
    let viewController = ShowPointsViewController() //this is the name of my ViewController
    let textFieldIndexPath = PointSourcesTV.indexPath(for: cell) //this is the name of my TableView

    print(textFieldIndexPath) //Returns nil here if I scroll too quickly
    let key = tableHeaders[(textFieldIndexPath?.section)!]
}
Sami
  • 579
  • 5
  • 25

1 Answers1

1

I'm not sure if you are using the correct method to get the indexPath for the UITextField and that may be causing the crash. But you could try something like in your cellForRowAtIndexPath you can set the tag of the UITextField to be the indexPath.row and then get the cell instance using-

let cell = PointSourcesTV.cellForRowAtIndexPath(IndexPath(row: textField.tag, section: 0)) as! UITableViewCell
Rikh
  • 4,078
  • 3
  • 15
  • 35
  • Thanks Rikh. This was the approach I originally wanted to take but since I have a variable amount of sections I was not sure how to pass this information into the `textFieldDidEndEditing`. I need information for both the `indexPath.row` and the `indexPath.section` in the `textFieldDidEndEditing` so I can edit my datasets correctly. I see here in the response the `section` is set to 0. Is there another way I can pass this information along variably? – Sami Nov 16 '16 at 05:03
  • Well if it were objective - c i could tell you to subclass your `UITextField` and pass two properties! But I am not efficient enough in swift to tell you how subclassing works and type the code for it in swift :)! – Rikh Nov 16 '16 at 05:13
  • I didn't think to do a `subclass`. doi! I'll give it a try later and let you know if it solves my issue. I'm definitely still interested in knowing why my code above only works when the `UITextFields` are on the sceen – Sami Nov 16 '16 at 17:02
  • It may be because when you're scroll fast and when you press the textField, owing to the re-use of the cells, the cell might not be properly setup by the time you tap it. Of course i'm just spitballing, maybe someone else can give the reason behind it :D – Rikh Nov 16 '16 at 17:22