How can I access a text field located in a table view cell outside of the cell?

3685 views ios
-2

After searching around, I couldn't find a solution that worked for me. I have a couple of text fields in a table cell. What I am trying to accomplish is changing those fields from hidden to visible with a leadingSwipe.

Code for cellForRowAt:

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
    let newCategory = cell.viewWithTag(5) as! UITextField
    let newAmount = cell.viewWithTag(6) as! UITextField
    newCategory.isHidden = true
    newAmount.isHidden = true
    return cell
}

Code for leadingSwipe:

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let indexPath = IndexPath(row: 0, section: 0)
    let cell = tableView.cellForRow(at: indexPath)
    let contextItem = UIContextualAction(style: .normal, title: "Edit") { (contextualAction, view, boolValue) in
        cell.newCategory.isHidden = false
        cell.newAmount.isHidden = false
        boolValue(false)
    }
    let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])

    return swipeActions
}

I am receiving the error "Value of type 'UITableViewCell?' has no member 'newCategory'" and similar for the newAmount.

answered question

2 Answers

10

You need to cast to the appropraite type and declare the properties as outlets

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableCell
    cell.newCategory.isHidden = true
    cell.newAmount.isHidden = true
    return cell
}

Where

class TableCell:UITableViewCell {

  @IBOutlet weak var newCategory:UITextField!
  @IBOutlet weak var newAmount:UITextField!

  override func awakeFromNib() {
    super.awakeFromNib()
  }

}

and you register it if it's programmatically created / with xib , if it's a prototype cell then don't register and only set reuse identifier to "cell"

BTW: you may have a condition inside cellForRowAt , as according to your setup the textfields will be hidden in all the cells

//

From -> File->New->File->CocaTouchClass do , and click Next

enter image description here

//

enter image description here

//

enter image description here

//

then register the cell in viewDidLoad

 tableView.register(UINib(nibName: "TableCell", bundle: nil), forCellReuseIdentifier: "cell") 
 tableView.delegate = self 
 tableView.dataSource = self

posted this
8

Moving let newCategory = cell.viewWithTag(5) as! UITextField and let newAmount = cell.viewWithTag(6) as! UITextField into the leadingSwipe function did the job.

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
   let indexPath = IndexPath(row: 0, section: 0)
   let cell = tableView.cellForRow(at: indexPath)
   let newCategory = cell.viewWithTag(5) as! UITextField
   let newAmount = cell.viewWithTag(6) as! UITextField
   let contextItem = UIContextualAction(style: .normal, title: "Edit") { (contextualAction, view, boolValue) in
    newCategory.isHidden = false
    newAmount.isHidden = false
    boolValue(false)
}
let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])

return swipeActions
}

posted this

Have an answer?

JD

Please login first before posting an answer.