Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 538599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:02:56+00:00 2026-05-13T10:02:56+00:00

I want to use two tableviews under the segmentedcontrol. one is plain styled and

  • 0

I want to use two tableviews under the segmentedcontrol. one is plain styled and the other is groupstyled. How can i controll the delegate and datasource for the two table views?

—
Regards,
Syed yusuf

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:02:56+00:00Added an answer on May 13, 2026 at 10:02 am

    Since you can’t change the UITableViewStyle of a UITableView once created (it can only be set at construction time), you have to have two different instances. You can do this in very different ways, but I’ve done it this way: add a UISegmentedControl to your interface, and set its target to the RootViewController class instance in your application. The RootViewController class could look like this:

    @class DataSource;
    
    @interface RootViewController : UITableViewController 
    {
    @private
        UITableView *_originalTableView;
        UITableView *_secondaryTableView;
        DataSource *_dataSource;
        BOOL _showingSecondaryTableView;
    }
    
    - (IBAction)swap:(id)sender;
    
    @end
    

    And this might be the implementation:

    #import "RootViewController.h"
    #import "DataSource.h"
    
    @implementation RootViewController
    
    - (void)dealloc 
    {
        [_dataSource release];
        [_originalTableView release];
        [_secondaryTableView release];
        [super dealloc];
    }
    
    - (void)viewDidLoad 
    {
        [super viewDidLoad];
    
        _dataSource = [[DataSource alloc] init];
    
        _secondaryTableView = [[UITableView alloc] initWithFrame:self.tableView.frame 
                                                           style:UITableViewStyleGrouped];
        _secondaryTableView.delegate = _dataSource;
        _secondaryTableView.dataSource = _dataSource;
    
        _originalTableView = [self.tableView retain];
    
        _showingSecondaryTableView = NO;
    }
    
    - (void)didReceiveMemoryWarning 
    {
        [super didReceiveMemoryWarning];
    }
    
    #pragma mark -
    #pragma mark IBAction method
    
    - (IBAction)swap:(id)sender
    {
        if (_showingSecondaryTableView)
        {
            self.tableView = _originalTableView;
            _showingSecondaryTableView = NO;
        }
        else
        {
            self.tableView = _secondaryTableView;
            _showingSecondaryTableView = YES;
        }
    }
    
    #pragma mark -
    #pragma mark Table view methods
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView 
     numberOfRowsInSection:(NSInteger)section 
    {
        return 5;
    }
    
    - (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] autorelease];
        }
    
        cell.textLabel.text = [NSString stringWithFormat:@"RootViewController cell %d", indexPath.row];
        return cell;
    }
    
    @end
    

    This is the interface of the DataSource class:

    #import <UIKit/UIKit.h>
    
    @interface DataSource : NSObject <UITableViewDelegate, 
                                      UITableViewDataSource>
    {
    }
    
    @end
    

    And the implementation:

    #import "DataSource.h"
    
    @implementation DataSource
    
    #pragma mark -
    #pragma mark Table view methods
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    {
        return 2;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView 
     numberOfRowsInSection:(NSInteger)section 
    {
        return 3;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView 
             cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    
        static NSString *CellIdentifier = @"DataSourceCell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                           reuseIdentifier:CellIdentifier] autorelease];
        }
    
        cell.textLabel.text = [NSString stringWithFormat:@"DataSource cell %d", indexPath.row];
        return cell;
    }
    
    @end
    

    You can change the datasource and delegate of the UITableView instances to anything you want, at any time during runtime, which might help you encapsulate different data sources with separate controllers.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.