Sam Claus
Sam Claus

Reputation: 1989

How to programmatically populate a static UITableView from the storyboard?

I am trying to create a UITableView similar to the About section of the iOS settings. I have my initial table view, which I populate dynamically (already working), and which has disclosure indicators for each of its items. When I click on an item in this view, I tell the navigation controller to slide over to a static UITableView (pictured below). I instantiate the static table view from the storyboard using its ID, and before I transition to it, I'd like to fill it with dynamic values according to which item it's describing from the initial table view. Basically, how do I set the Right Detail portions of cells in a static table view in my code.?

The static UITableView.

The pseudocode I'm looking for goes something like this (exists inside the controller class for the initial table view):

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

  let customData: [String:String] = getDataForCell(atIndexPath: indexPath)
  let descriptionView = storyboard?.instantiateViewControllerWithIdentifier("keyDetails") as! KeyDetailsController

  descriptionView.getCell(forRow: 0, inSection: 0).rightDetail.text = customData["serverName"]! // changes "A Fake Server" to an actual server name

}

Upvotes: 1

Views: 1553

Answers (1)

Rob
Rob

Reputation: 437882

Just to summarize our discussion in chat: To update those labels, you just create IBOutlet reference to the labels.

The key issue here is that you're trying to update these outlets immediately after instantiating the view controller. But the outlets are not configured until the view is loaded, something that happens during the process of transitioning to the new scene. So you should:

  • Create String (or whatever) properties in the destination controller;

  • Populate these properties after the view controller is instantiated. Where yo do this depends upon how you're transitioning to the next scene:

    • If transitioning to the next scene programmatically (via showViewController, presentViewController or navigationController.pushViewController), then set these right after you call instantiateViewControllerWithIdentifier; or

    • If using a segue, set these String properties in prepareForSegue.

  • In viewDidLoad of the destination view controller, take these String properties and update the controls for which you have `IBOutlet references.

For more information, see Passing Data between View Controllers.

Upvotes: 2

Related Questions