简体   繁体   中英

How can I add 2 UITableView on 1 nib/UIView but using 2 different UITableViewController to handle?

How can I add 2 UITableView on 1 nib/UIView but using 2 different UITableViewController to handle ?

Thanks for helping!


Updated:


I understand the basic idea, but I just can't get it working together.

Maybe it is a simple question, but for someone doesn't know, it is obviously a difficult one.

Anyone can help trouble shooting a little bit?

I created a view based application named "MutiTableView",then drag & drop 2 TableView into the nib.

This is the generate ViewController, I add 2 IBOutlet in order to connect with 2 tables in the nib.

@class FirstTableViewController;
@class SecondTableViewController;
@interface MutiTableViewViewController : UIViewController {

    UITableView *tablefirst;
    UITableView *tablesecond;
    FirstTableViewController *firstController;
    SecondTableViewController *secondController;
}

//@property (nonatomic, retain) IBOutlet Table1ViewController *viewController;
@property (nonatomic, retain) IBOutlet UITableView *tablefirst;
@property (nonatomic, retain) IBOutlet UITableView *tablesecond;
@property (nonatomic, retain)  FirstTableViewController *firstController;
@property (nonatomic, retain)  SecondTableViewController *secondController;

This is how I set the datasource and delegate

- (void)viewDidLoad {
    NSLog(@"viewDidLoad :(");
    firstController = [[FirstTableViewController alloc] initWithStyle:UITableViewStylePlain];
    secondController = [[SecondTableViewController alloc] initWithStyle:UITableViewStylePlain];

    tablefirst.delegate = firstController;
    tablefirst.dataSource = firstController;

    tablesecond.delegate = secondController;
    tablesecond.dataSource = secondController;

    [super viewDidLoad];
}

This is my FirstTableViewController, it is just a basic tableViewController

@interface FirstTableViewController : UITableViewController {
    NSArray *listData;
}
@property (nonatomic,retain) NSArray * listData;
@end

#import "FirstTableViewController.h"


@implementation FirstTableViewController
@synthesize listData;
/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/


- (void)viewDidLoad {
    NSLog(@"FirstTableViewController viewDidLoad");
    NSArray * array = [[NSArray alloc] initWithObjects:@"111",@"222",@"333",@"444",@"555",@"666",@"777",@"888",@"999",@"000",nil];
    self.listData = array;
    [array release];
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

.....

I also implemented the methods like following.

numberOfSectionsInTableView
tableView:numberOfRowsInSection:
tableView:cellForRowAtIndexPath:

Set the delegate and datasource to different UITableViewControllers.

AUITableViewController * firstController = [[AUITableViewController alloc] init];
UITableView * table1 = [[UITableView alloc] initWith...];
table1.delegate = firstController;
table1.dataSource = firstController;
[someView addSubview:table1];
[table1 release];
[firstController release];

AnotherUITableViewController * secondController = [[AnotherUITableViewController alloc] init];
UITableView * table2 = [[UITableView alloc] initWith...];
table2.delegate = secondController;
table2.dataSource = secondController;
[someView addSubview:table2];
[table2 release];
[secondController release];

It's not tested, but that's the basic idea.

To do it in interface builder, just connect the DataSource and Delegate connectors to different classes / controllers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM