Can anyone tell me how I can show multiple UITableViews in a single view?
-
1if 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.sandy– sandy2011-11-02 13:17:33 +00:00Commented Nov 2, 2011 at 13:17
-
you want both table views to see together or one at a time(one hidden and one visible)..?mAc– mAc2011-11-02 13:19:56 +00:00Commented 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 ?Maulik– Maulik2011-11-02 13:21:12 +00:00Commented Nov 2, 2011 at 13:21
-
Hope this helps, but i haven't votedown you...mAc– mAc2011-11-02 13:21:35 +00:00Commented Nov 2, 2011 at 13:21
-
do one thing edit your question and explain your problem more openly, so that we can help you.mAc– mAc2011-11-02 13:22:37 +00:00Commented Nov 2, 2011 at 13:22
3 Answers
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
}
}
1 Comment
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
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