Search code examples
iosswiftxcodeiboutlet

Swift 3. Trying to update from viewController some IBOutlets declared in prototype cell class


I have cells in a tableView with 2 labels displaying text. Cells are managed in a detailCell class of type UITableViewCell, that references IBOutlets for both labels. This allows me to manage my prototype cell and display core data entries.

I added UITextField (with border style invisible) on top of each label and I would like to hide the label when user taps the textField and starts editing.

Problem is I cannot figure how to manage the label (referenced in the prototype class) from my viewController without referencing a second time the label in the viewController. Of course this fires the error: "Outlets cannot be connected to repeating content".

The other way round would be to hide/display label by managing it from the cell class, according to the var isEditing: Bool that I use in the viewController, but I do not manage to use it in the cell class.

Any ideas?


Solution

  • To achieve this you have to do something like below:

    -> First of all you have to set all of your text fields delegate to self

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    
    let aCell : myCustomCell = tableView.dequeueReusableCell(withIdentifier: "aCell") as! myCustomCell
    
            aCell.txtView1.delegate = self
        return aCell    
        }
    

    -> Secondly you have to get particular cell when user click on textfiels

         func textFieldDidBeginEditing(_ textField: UITextField) {
    
              //Get parent cell by traveling to the superview as your cell (Depends on your view hierarchy in your cell)
                let aCell : myCustomCell = textField.superview?.superview as! myCustomCell
    
    //Hide below label
    aCell.lblData.hidden = true
    
            }
    

    --> Third, you have to hide text field and show label again when text field end editing get called

    func textFieldDidEndEditing(_ textField: UITextField) {
            let aCell : myCustomCell = textField.superview?.superview as! myCustomCell
    
        //Hide below label
        aCell.lblData.text = textField.text
        aCell.lblData.hidden = false
        }