title: Set the value of UIPickerView right after its data is loaded
body: <p>I have developed something very similar to DateCell Demo from Apple. <a href="https://developer.apple.com/library/ios/samplecode/DateCell/Introduction/Intro.html" rel="nofollow">https://developer.apple.com/library/ios/samplecode/DateCell/Introduction/Intro.html</a></p>
  
  <p>The only difference is that I have a UIPickerView instead of a UIDatePicker. And whenever the cell is touched a new UIPickerView is instantiated and added to the cell below once touched again it is removed from the cell. And the part I have problem with is to select a specific item in the UIPickerView. </p>
  
  <p>Here is some code:</p>
  
  <pre><code>var selectedPlatform = "Mac"
  var platformPickerDataSource = ["iOS", "Mac", "PC", "Android"]
  
  
  func updatePlatformPicker()
  {
      if let indexPath = platformPickerIndexPath {
          let associatedPlatformPickerCell = tableView.cellForRowAtIndexPath(indexPath)
          if let targetedPlatformPicker = associatedPlatformPickerCell?.viewWithTag(Constants.kPlatformPickerTag) as! UIPickerView? {
  
              targetedPlatformPicker.dataSource = self
              targetedPlatformPicker.delegate = self
              // I cannot do selectRow(selectedPlatform, inComponent: 0,
    animated : true) here since the row since the rows in the UIPickerView are not set
          }
      }
  }
  </code></pre>
  
  <p>So my questions is, if you refer to the code from Apple, where can I set?</p>
  
  <pre><code>selectRow(selectedPlatform, inComponent: 0,
    animated : true)
  </code></pre>
  
  <p>Edit: (I added the whole code, which is basically the swift version of Apple's DateCell Example and DatePicker replaced with a UIPickerView)</p>
  
  <pre><code>import UIKit
  
  class PickerTableViewController: UITableViewController, UIPickerViewDataSource, UIPickerViewDelegate {
  
  var platformPickerIndexPath : NSIndexPath?
  
  
  var pickerCellRowHeight: CGFloat = 140
  
  
  var selectedPlatform = "Mac"
  var platformPickerDataSource = ["iOS", "Mac", "PC", "Android"]
  
  private struct Constants {
      static let kPlatformCellID: String = "platformCell"
      static let kPlatformPickerID: String = "platformPicker"
      static let kOtherCell: String = "otherCell"
      static let kPlatformRow : Int = 1
      static let kPlatformPickerTag : Int = 99
  }
  
  override func viewDidLoad() {
      super.viewDidLoad()
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
  
  // MARK: - Table view data source
  
  override func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
      return 1
  }
  
  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      var rows = 3
  
      if hasInlinePlatformPicker()
      {
          rows = 4
      }
      return rows
  }
  
  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
  
      var cell : UITableViewCell?
  
      print("\(indexPath.row)")
  
      var cellID = Constants.kOtherCell
  
      if indexPathHasPicker(indexPath)
      {
          cellID = Constants.kPlatformPickerID
      }
      else if indexPathHasPlatform(indexPath)
      {
          cellID = Constants.kPlatformCellID
      }
  
      cell = tableView.dequeueReusableCellWithIdentifier(cellID)
  
      var modelRow = indexPath.row
      if (platformPickerIndexPath != nil &amp;&amp; platformPickerIndexPath?.row &lt;= indexPath.row) {
          modelRow -= 1
      }
  
      if cellID == Constants.kPlatformCellID
      {
          cell?.textLabel?.text = "Select a platform"
          cell?.detailTextLabel?.text = selectedPlatform
      }
      else if cellID == Constants.kOtherCell
      {
          cell?.textLabel?.text = "Other Cell"
      }
      return cell!
  }
  
  
  func indexPathHasPicker(indexPath: NSIndexPath) -&gt; Bool
  {
      return hasInlinePlatformPicker() &amp;&amp; platformPickerIndexPath?.row == indexPath.row
  }
  
  func indexPathHasPlatform(indexPath : NSIndexPath) -&gt; Bool
  {
      var hasPlatform = false
      if indexPath.row == Constants.kPlatformRow || (hasInlinePlatformPicker() &amp;&amp; (indexPath.row == Constants.kPlatformRow + 1))
      {
          hasPlatform = true
      }
      return hasPlatform
  }
  
  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  
      let cell = tableView.cellForRowAtIndexPath(indexPath)
  
      if cell?.reuseIdentifier == Constants.kPlatformCellID
      {
          displayInlinePlatformPickerForRowAtIndexPath(indexPath)
      }
      else
      {
          tableView.deselectRowAtIndexPath(indexPath, animated: true)
      }
  
  }
  
  func displayInlinePlatformPickerForRowAtIndexPath(indexPath : NSIndexPath)
  {
      // display the platform picker inline with the table content
      tableView.beginUpdates()
      var before = false
  
      if hasInlinePlatformPicker()
      {
          before = platformPickerIndexPath?.row &lt; indexPath.row
      }
  
  
      let sameCellClicked = (platformPickerIndexPath?.row == indexPath.row + 1)
  
      // remove any picker cell if it exists
      if hasInlinePlatformPicker()
      {
  
          tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: platformPickerIndexPath!.row, inSection: 0)], withRowAnimation: .Fade)
          platformPickerIndexPath = nil
      }
  
      if (!sameCellClicked)
      {
          // hide the old picker and display the new one
  
          let rowToReveal = (before ? indexPath.row - 1 : indexPath.row)
          let indexPathToReveal = NSIndexPath(forRow: rowToReveal, inSection: 0)
  
  
          togglePlatformPickerForSelectedIndexPath(indexPathToReveal)
  
          platformPickerIndexPath = NSIndexPath(forRow: indexPathToReveal.row + 1, inSection: 0)
      }
  
      tableView.deselectRowAtIndexPath(indexPath, animated: true)
  
      tableView.endUpdates()
  
      updatePlatformPicker()
  }
  
  
  func updatePlatformPicker()
  {
  
  
      if let indexPath = platformPickerIndexPath {
          let associatedPlatformPickerCell = tableView.cellForRowAtIndexPath(indexPath)
          if let targetedPlatformPicker = associatedPlatformPickerCell?.viewWithTag(Constants.kPlatformPickerTag) as! UIPickerView? {
  
              targetedPlatformPicker.dataSource = self
              targetedPlatformPicker.delegate = self
  
          }
      }
  }
  
  
  func togglePlatformPickerForSelectedIndexPath(indexPath: NSIndexPath)
  {
  
      tableView.beginUpdates()
  
      let indexPaths = [NSIndexPath(forRow: indexPath.row + 1, inSection: 0)]
  
  
  
  
      // check if 'indexPath' has an attached picker below it
      if hasPickerForIndexPath(indexPath)
      {
          // found a picker below it, so remove it
          tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Fade)
      }
      else
      {
          // didn't find a picker below it, so we should insert it
          tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Fade)
  
  
      }
  
      tableView.endUpdates()
  
  
  }
  
  
  func hasPickerForIndexPath(indexPath: NSIndexPath) -&gt; Bool
  {
  
      var hasPlatformPicker = false
      let targetedRow = indexPath.row + 1
  
      let checkPlatformPickerCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: targetedRow, inSection: 0))
  
      let checkPlatformPicker  = checkPlatformPickerCell?.viewWithTag(Constants.kPlatformPickerTag)
  
      hasPlatformPicker = checkPlatformPicker != nil
      return hasPlatformPicker
  }
  
  
  
  func hasInlinePlatformPicker() -&gt; Bool
  {
      return platformPickerIndexPath != nil
  }
  
  
  override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -&gt; CGFloat {
      return (indexPathHasPicker(indexPath) ? pickerCellRowHeight : tableView.rowHeight)
  }
  
  // MARK: - Platform Picker View Data Source and Delegates
  func numberOfComponentsInPickerView(pickerView: UIPickerView) -&gt; Int {
      return 1
  }
  func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -&gt; Int {
      return platformPickerDataSource.count
  }
  
  func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -&gt; String? {
      return platformPickerDataSource[row]
  }
  
  func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
      print("Platform Selected: \(platformPickerDataSource[row])")
  
      var targetedCellIndexPath: NSIndexPath?
  
      if hasInlinePlatformPicker() {
          // inline picker: update the cell's above the picker cell
          //
          targetedCellIndexPath = NSIndexPath(forRow: platformPickerIndexPath!.row - 1, inSection: 0)
      }
  
      let cell = tableView.cellForRowAtIndexPath(targetedCellIndexPath!)
  
      cell?.detailTextLabel?.text = platformPickerDataSource[row]
  }
  
  
  }
  </code></pre>