title: Table view reload from segmented control
body: <p>A few additional points to clarify the question below:</p>
  
  <p>All this happens in a single view controller which does core data as well as the main tableview access. I have done a little more digging (just as I am really not sure what loads when in an iOS app) and now know a little more. The reload call is actually working (as if I hard code changes to the cell and call reload data the table view changes). The sort works fine on viewdidload and invokes the fetchedresultsController variable when the program first loads. If I comment that section though with a println nothing prints except for on first load so the sort is never subsequently invoked when reloaddata is called and hence the view does not change because the view has not changed!! So, all that leads me to ask how I can invoke the sort the happens when the program kicks off manually? I appreciate your patience in dealing with dumb newbie questions</p>
  
  <p>I am working on a core data based app with a main table view, sorted by a main key. </p>
  
  <p>That all works ok and I have now added a segmented control to define how to resort the data (by date, by name, by value etc) and that works fine too in that the fetchedresultscontroller sees the selection and sorts accordingly (if I hard code the selection and end and restart the app). </p>
  
  <p>My problem is that I cannot figure out how to get the table view to reload when I change the segment selection. I have used <code>self.tableview.reloadData()</code> but it does nothing I can see. I suspect I am looking at this too simplistically and somehow have to invoke the sort/fetchedresultscontroller but am at a loss as to how to do this. Is there a specific method I need to invoke? I am relatively new to all this so go gently please! Code is below:</p>
  
  <pre><code>@IBAction func indexChanged(sender: UISegmentedControl) {
  
      switch segmentedControl.selectedSegmentIndex
  
      {
      case 0:
          segmentSortChoice = 0;
      case 1:
          segmentSortChoice = 1;
      case 2:
          segmentSortChoice = 2;
      case 3:
          segmentSortChoice = 3;
      default:
          break;
      }
      self.tableView.reloadData()
  }  
  </code></pre>
  
  <p>in addition - this is in the core data section - am I trying to invoke the sorts from here somehow?</p>
  
  <p>var fetchedResultsController: NSFetchedResultsController {
          if _fetchedResultsController != nil {
              return _fetchedResultsController!
          }</p>
  
  <pre><code>    let fetchRequest = NSFetchRequest()
  
      // edited to use the Opportunity entity
      let entity = NSEntityDescription.entityForName("Opportunity",
          inManagedObjectContext: self.managedObjectContext!)
      fetchRequest.entity = entity
  
      // Set the batch size to a suitable number.
      fetchRequest.fetchBatchSize = 20
  
      // edited to sort by opportunity name and then aaccount name;
      // both using case insensitive comparisons
      let oppNameSortDescriptor = NSSortDescriptor(key: "oppName",
          ascending: true, selector: "caseInsensitiveCompare:")
      let accNameSortDescriptor = NSSortDescriptor(key: "accName",
          ascending: true, selector: "caseInsensitiveCompare:")
      let valueSortDescriptor = NSSortDescriptor(key: "dollarValue",
          ascending: false, selector: "caseInsensitiveCompare:")
      let closeDateSortDescriptor = NSSortDescriptor(key: "closeDate",
          ascending: false, selector: "caseInsensitiveCompare:")
  
      // The section below will sort the table view based on the selection in the segmented view
  
  
          segmentSortChoice = NSUserDefaults.standardUserDefaults().valueForKey("SortPref") as! Int
  
  
      if segmentSortChoice == 0
      {fetchRequest.sortDescriptors =
          [oppNameSortDescriptor, accNameSortDescriptor]}
      if segmentSortChoice == 1
      {fetchRequest.sortDescriptors =
              [accNameSortDescriptor, oppNameSortDescriptor]}
      if segmentSortChoice == 2 {
              fetchRequest.sortDescriptors =
              [valueSortDescriptor, oppNameSortDescriptor]}
      if segmentSortChoice == 3 {
          fetchRequest.sortDescriptors =
              [closeDateSortDescriptor, oppNameSortDescriptor]}
  </code></pre>