Search code examples
iosios6

How to reload a UITableView after a segmented control changed?


I am trying to control context of tableview with segmented control(which is above tableview). On change segment I want to reload data in my table tableView with different cells ( I have two types of cells and two data arrays for every type). How to clear and reload data with different cell ?


Solution

  • I'm just providing a few lines of code to what @jaydee3 has mentioned.

    - (IBAction)segmentControlValueChanged:(UISegmentedControl *)sender
    {
        //Do something
        [self.tableView reloadData];
    }
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.segmentControl.selectedSegmentIndex?[self.dataArray2 count]:[self.dataArray1 count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if(self.segmentControl.selectedSegmentIndex){
           // Create Type cell for selected Index 1
        }
        else{
           // Create Type cell for selected Index 0
        }
    }