How can I Implement multiple uitableview in a sing

2020-08-03 04:16发布

问题:

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

回答1:

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
}
}


回答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];


回答3:

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



标签: iphone