How do I make textField editable in TableViewCell when the Edit button in NavigationBar is pressed?

Asked 2 years ago, Updated 2 years ago, 35 views

Xcode ver9.2, Swift is developing an iOS app.

When the Edit button in the upper right corner of NavigationBar is pressed, how do I make textField editable in TableViewCell?

Relationship between object placement and code (in parentheses):

  • NavigationController
    • TableViewController (TableViewController.swift)
      • TableViewCell (TableViewCell.swift)
        • TextField
  • TableViewController (TableViewController.swift)
    • TableViewCell (TableViewCell.swift)
      • TextField
  • TableViewCell (TableViewCell.swift)
    • TextField
  • TextField

In order to prevent editing of TextField in the initial display, awakeFromNib() in TableViewCell.swift is set to textField.isEnabled=false.

If the edit button is pressed, could you tell me how to set it to true so that I can edit TextField?

TableViewController.swift:

import UIKit

classTableViewController:UITableViewController, TableViewCellDelegate{

    @IBOutlet variableView:UITableView!

    var array: [String] = ["Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

    override func viewDidLoad(){
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearSelectionOnViewWillAppear=false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        self.navigationItem.rightBarButtonItem=self.editButtonItem
    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func setEditing(_editing:Bool, animated:Bool) {
        super.setEditing (editing, animated: animated)
    }

    // MARK: - Table view data source

    override func numberOfSections (installableView:UITableView) - > Int {
        // # warning incomplete implementation, return the number of sections
        return1
    }

    override functableView(_tableView:UITableView, numberOfRowsInSection section:Int)->Int{
        // # warning incomplete implementation, return the number of rows
        return array.count
    }

    override functableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath) - >UITableViewCell{
        letcell=tableView.dequeueReusableCell(withIdentifier: "inputCell", for:indexPath) as!TableViewCell
        cell.textField.text=array [indexPath.row]
        // Deligate Configuration
        cell.delegate=self
        return cell
    }

    // After editing the text field
    functextFieldDidEndEditing(cell:TableViewCell, value:String) - >(){
        let path=tableView.indexPathForRow(at:cell.convert(cell.bound.origin, to:tableView))
        array [(path?.row)!] = value
    }

    // After pressing the delete button
    override functableView(_tableView:UITableView, commit editingStyle:UITableViewCellEditingStyle, forRowAtindexPath:IndexPath){
        if(editingStyle==UITableViewCellEditingStyle.delete){
            array.remove(at:indexPath.row)
            tableView.deleteRows(at:[indexPath], with:.fade)
        }
    }

    override functableView(_tableView:UITableView, moveRowAtsourceIndexPath:IndexPath, to destinationIndexPath:IndexPath){

        letcell=tableView.cellForRow(at:sourceIndexPath)as!TableViewCell
        let moveData=cell.textField.text
        array.remove(at:sourceIndexPath.row)
        array.insert (moveData!, at:destinationIndexPath.row)
    }

}

TableViewCell.swift:

import UIKit

// protocol
protocolTableViewCellDelegate{
    functextFieldDidEndEditing (cell:TableViewCell, value:String) - >()
}

classTableViewCell:UITableViewCell, UITextFieldDelegate{

    vardelegate —TableViewCellDelegate!=nil

    // text field connection
    @IBOutlet weak var textField: UITextField!

    override funcawakeFromNib(){
        super.awakeFromNib()
        // text field derigate
        textField.delegate=self
        // Change the Text Field Input Keyboard Line Break to Complete
        textField.returnKeyType=.done
        // Set the text field to non-editable before pressing the edit button in the upper right corner
        textField.isEnabled=false
    }

    override func setSelected(_selected:Bool, animated:Bool) {
        super.setSelected(selected, animated:animated)

        // Configure the view for the selected state
    }

    // Deligate after editing text fields
    functextFieldDidEndEditing(_textField:UITextField){
        self.delegate.textFieldDidEndEditing (cell:self, value:textField.text!)
    }

    // DELIGATE AFTER RETURN KEY PRESS
    functextFieldShouldReturn(_textField:UITextField) - >Bool{
        textField.resignFirstResponder()
        return true
    }

}

Edit Screen:
Enter a description of the image here

swift ios

2022-09-30 19:22

1 Answers

Delete textField.isEnabled=false in cell awakeFromNib() and cell.textField.isEnabled=self.isEditing in CellForRowAt in TableViewController.


2022-09-30 19:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.