iosswiftuitableviewcore-datacustom-cell

How do you insert content to a custom status cel which contains a UITextField?


I have two view controller that for adding my "Routines". One is to display a list of all routines and one to edit/create routines.

In the first view controller I have two way to enter to the second controller

  1. by selecting an already created routine
  2. by pressing the "+" button on the navigation bar

If the press option is pressed the data should be recalled from CoreData entity and the fields in the second view controller shall be populated.

But I get the error: fatal error: unexpectedly found nil while unwrapping an Optional value

Would anyone know how to insert content to a custom status cell named "NameCell" which contains a UITextField?

Thanks in advance, Ace

enter image description here

enter image description here


Solution

  • If you are using static cells in your table view in the storyboard, but want to populate data in one of them, one does not implement any of the UITableViewDataSource methods, but rather just create IBOutlet from the UILabel in question, and then you can populate it as you see fit.

    class ViewController: UITableViewController {
    
        @IBOutlet weak var foo: UILabel!
        var bar: String!  // this is populated by `prepareForSegue` of presenting view controller
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            foo.text = bar
        }
    
    }
    

    As the Table View Programming Guide for iOS says:

    Note: If a table view in a storyboard is static, the custom subclass of UITableViewController that contains the table view should not implement the data source protocol. Instead, the table view controller should use its viewDidLoad method to populate the table view’s data. For more information, see Populating a Static Table View With Data.