title: Segmented Control change tableView datasource
body: <p>UISegmentedControl changes tableView datasource.<br>
  After I described <code>[myView addSubview:_tableView];</code> <br>
  instead of <code>[self.view addSubview:_tableView];</code>,<br>
  <code>daySegmentedControl</code> process stopped working.</p>
  
  <p>I have this code.</p>
  
  <p>ViewController.m</p>
  
  <pre><code>@implementation ViewController
  {
      int _tableType;
      NSArray *_data1;
      NSArray *_data2;
  }
  
  @synthesize segment;
  @synthesize daySegment;
  @synthesize myView;
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
      _tableType = 1;
      _data1 = @[@[@"A",@"B",@"C"]];
      _data2 = @[@[@"D",@"E",@"F"]];
  
      myView = [[UIView alloc]initWithFrame:CGRectMake(-1, 44, 340, 480)];
      [self.view addSubview:myView];
      myView.opaque = NO;
      myView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.0f];
      [self.view bringSubviewToFront:myView];
  
      [self segmentView];
      [self daySegmentView];
      [self dayTableView];
  }
  
  - (void)segmentView
  {
      NSArray *SegmentContent = [NSArray arrayWithObjects:@"View1",@"View2",nil];
      segment = [[UISegmentedControl alloc] initWithItems:WDSegmentContent];
      segment.frame = CGRectMake(-2, 20, 326, 25);
      segment.selectedSegmentIndex = 0;
      [segment addTarget:self action:@selector(WDSegmentAction:) forControlEvents:UIControlEventValueChanged];
      [self.view addSubview:segment];
  }
  
  - (void)WDSegmentAction:(id)sender
  {
      switch (segment.selectedSegmentIndex){
          case 0:
              [self dayTableView];
              break;
  
          case 1:
              [self dayTableView];
              break;
  
          default:
              break;
      }
  }
  
  - (void)daySegmentView
  {
      NSArray *daySegmentContent = [NSArray arrayWithObjects:@"A",@"D",nil];
      daySegment = [[UISegmentedControl alloc] initWithItems:daySegmentContent];
      daySegment.frame = CGRectMake(0, 0, 326, 25);
      daySegment.selectedSegmentIndex = 0;
      [daySegment addTarget:self action:@selector(daySegmentAction:) forControlEvents:UIControlEventValueChanged];
      [myView addSubview:daySegment];
  }
  
  - (void)daySegmentAction:(id)sender
  {
      switch (segment.selectedSegmentIndex){
          case 0:
              _tableType = 1;
              [self.tableView reloadData];
              break;
  
          case 1:
              _tableType = 2;
              [self.tableView reloadData];
              break;
  
          default:
              break;
      }
  }
  
  - (void)dayTableView
  {
      _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, 320, 480)];
      _tableView.dataSource = self;
      _tableView.delegate = self;
      [myView addSubview:_tableView];
  }
  
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
      return 1;
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
      return [_data1[section]count];
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      }
  
      NSString *data;
      if (_tableType == 1) {
          data = _data1[indexPath.section][indexPath.row];
      } else if (_tableType == 2){
          data = _data2[indexPath.section][indexPath.row];
      }
      cell.textLabel.text = data;
  
      return cell;
  }
  
  @end
  </code></pre>
  
  <p>Any idea on how I could fix it?</p>