0

Can anyone tell me how I can show multiple UITableViews in a single view?

6
  • 1
    if any one do not know then please don't reduce reputation. because I'm in such situation that I need to implement two uitableview in single view. may be my question is wrong but my need is correct. Commented Nov 2, 2011 at 13:17
  • you want both table views to see together or one at a time(one hidden and one visible)..? Commented Nov 2, 2011 at 13:19
  • two table view means ? did you mean same table view with different data or two different tables that appears together with different data ? Commented Nov 2, 2011 at 13:21
  • Hope this helps, but i haven't votedown you... Commented Nov 2, 2011 at 13:21
  • do one thing edit your question and explain your problem more openly, so that we can help you. Commented Nov 2, 2011 at 13:22

3 Answers 3

4

1) Draw different table views using different frames/ Drag and drop table views of different sizes, if using XiB.

2) Conform to table view protocols as usual and give implementation for delegate/datasource methods

3) In the delegate/datasource methods decide for which table view, it was called, using the table view's object. for example:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    if(tableView == tableView1)
{
        //Do this
}
else if(tableView == tableView2)
{
        //Do that
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Do this. This implies that you have "tableView1" and "tableView2" set up as ivars or perhaps as @properties. Having one set of delegate/data source methods that choose what data to respond with is going to keep you way saner than trying to build separate table management classes.
2

To show multiple UITableView in a single view, you can Instantiate multiple UITableView and add them as subviews, like this:

UITableView *tb1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 200, 100) style:UITableViewStylePlain];
UITableView *tb2 = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, 200, 100) style:UITableViewStylePlain];
UITableView *tb3 = [[UITableView alloc] initWithFrame:CGRectMake(0, 200, 200, 100) style:UITableViewStylePlain];

[self.view addSubview:tb1];
[self.view addSubview:tb2];
[self.view addSubview:tb3];

[tb1 release];
[tb2 release];
[tb3 release];

Comments

1

you will need to implement multiple tableView data source. create new NSObject class for each table view:

in DataSourceOne.h:

#import <Foundation/Foundation.h>


    @interface DataSourceOne : NSObject <UITableViewDataSource, UITableViewDelegate> {
         NSMutableArray *data;
    }
    @property (nonatomic, retain) NSMutableArray *data;

    - (id)initWithData:(NSMutableArray *)d;

    @end

Then, in every *.m files of data source classes implement source of each table view data. Then, in ViewController class, which contains your table Views:

ViewController.h:

#import "DataSourceOne.h"
#import "DataSourceTwo.h"
#import "DataSourceThree.h"


@interface SearchView : UIViewController {
    DataSourceOne *ds1;
    DataSourceTwo *ds2;
    DataSourceThree *ds3; 
UITableView *table1;
UITableView *table2;
UITableView *table3;
}
@property (nonatomic, retain) IBOutlet UITableView *table1;
@property (nonatomic, retain) IBOutlet UITableView *table2;
@property (nonatomic, retain) IBOutlet UITableView *table3;
@end

Finaly, set data sources and delegates to every UITableView:

ViewController.m:


    - (void)viewDidLoad
    {
    ds1 = [[DataSourceOne alloc] init];
    [table1 setDataSource:ds1];   //for data source
    [table1 setDelegate:da1];     //for callbacks (didSekectRowAtIndexPath)
    ...
    }

You may even change gata source for every tableView at any time: just set new datasource and delegete to it. GL&HF

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.