Anushka
Anushka

Reputation: 53

How to reload tableview data one array to another array?

I am trying to create single table view with dual NSMutableArray values. now I have maintaining UISegmentcontrol first button click to load first NSMutableArray data. Second button click to load second NSMutableArray data values(after clicking second button first data should not display).

NSMUtableArray *firstarray = [First values];
NSMUtableArray *secondarray = [second values];


-(void)segmentedControlValueDidChange:(UISegmentedControl *)segment
{
    switch (segment.selectedSegmentIndex) {
        case 0:{
            //action for the first button (Current)
           //want to load first array data into table
            break;}
        case 1:{
            //action for the first button (Current)
           //want to load second array data into table
           break;}
    }
}

Upvotes: 1

Views: 1101

Answers (4)

tuledev
tuledev

Reputation: 10317

Create 1 array to hold the data for table, so you don't have to check (using if, ...) in every delegate methods of UITableView:

NSMutableArray * arrTableData;

-(void)segmentedControlValueDidChange:(UISegmentedControl *)segment
{
    switch (segment.selectedSegmentIndex) {
        case 0:{
            arrTableData = firstarray;
           [tableView reloadData];

            break;}
        case 1:{
             arrTableData = secondarray;
             [tableView reloadData];

           break;}
    }
}

Using arrTableData for delegates of UITableView: numberOfRowsInSection, cellAtIndex...

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

   return arrTableData.count;

}

Upvotes: 4

Arslan Asim
Arslan Asim

Reputation: 1302

Create a bool variable say isLoadFirst then in your Following Code

-(void)segmentedControlValueDidChange:(UISegmentedControl *)segment
{
switch (segment.selectedSegmentIndex) {
    case 0:{
         isFirstLoad  = YES;
         [yourTableView reloadData];
        //action for the first button (Current)
       //want to load first array data into table
        break;}
    case 1:{
        //action for the first button (Current)
       //want to load second array data into table
        isFirstLoad  = NO;
         [yourTableView reloadData];
       break;}
}
}

Now in your

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     if (isFirstLoad)
   return firstArrayCount;
   else 
   return secondArrayCount
}

And same check for

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Upvotes: 1

Sauvik Dolui
Sauvik Dolui

Reputation: 5660

Implement your UITableViewDelegate in this following way

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   if (mySegmentContorll.selectedSegmentIndex == 0) {
       // Draw Cell content with data taken from first array, data = firstarray[indexPath]
   } else if (mySegmentContorll.selectedSegmentIndex == 1) {
      // Draw Cell content with data taken from second array, data = secondarray[indexPath]
   }

}

Also call [tableView reloadData]; when segmented control changes.

Upvotes: 1

Mradul Kumar
Mradul Kumar

Reputation: 357

You can use an enum to check which button you pressed. For button one set enum value to 1, and for button two set enum value to 2. In in table view delegate methods you can check currently selected enum and return the appropriate value.

For example :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger count = 0;


if(m_choice == 1)
    count = [firstarray count];
else if(m_choice == 2)
    count = [secondarray count];

return count;}

And do this to other table view methods too.

Upvotes: 2

Related Questions