title: IOS Number Of Sections In Table View Dynamic
body: <p>I am creating an app where users can store an object in Core Data.  I have the Objects being pulled into a UITableView and everything is working correctly there.  I now want to separate the objects into a possible of 1-3 different sections based on choices in a UISegmentedControl.</p>
  
  <p>Currently I have this to create the 1 section and populate the cells of that section with my objects</p>
  
  <pre><code>#pragma mark - Table view data source
  
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
      // Return the number of sections.
      return 1;
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
      // Return the number of rows in the section.
      return self.fetchedDiscs.count;
  }
  
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      static NSString *CellIdentifier = @"DiscCell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  
      //Configure Cell
      Disc *currentDisc = [self.fetchedDiscs objectAtIndex:indexPath.row];
  
      cell.textLabel.text = currentDisc.name;
      cell.detailTextLabel.text = currentDisc.brand;
  
      return cell;
  }
  </code></pre>
  
  <p>So the main question is how to dynamically change the number of sections and number of rows in section?</p>
  
  <p>The Segmented control returns values such as   Option 1, Option 2, Option 3.  The only way I can think of is to loop through my fetchedDiscs array and separate that into an array for each section.  Then I can return the number of arrays if they exists and I can get the count of each array to get the number of rows in each section.   But then I get to the problem of How to get the CellForRowAtIndextPath to work correctly with three arrays.</p>
  
  <p>Basically there has to be a better more logical way to do this.  I am just not sure how.</p>