Я думаю, что лучший способ сделать это, вместо того, чтобы использовать три отдельных контроллера, просто использовать один. Этот контроллер будет подклассом UIViewController
, а его файл .h
должен выглядеть примерно так:
@interface MyController : UIViewController <UITableViewDataSource, UITableViewDelegate>
UITableView *firstTableView;
UITableView *secondTableView;
@end
Теперь в вашем файле .m
настройте таблицы следующим образом:
- (id)init {
if ((self = [super init])) {
firstTableView = [[UITableView alloc] initWithFrame:/*desired frame*/ style:/*desired style*/];
secondTableView = [[UITableView alloc] initWithFrame:/*desired frame*/ style:/*desired style*/];
firstTableView.delegate = self;
firstTableView.dataSource = self;
secondTableView.delegate = self;
secondTableView.dataSource = self;
}
}
- (void)dealloc {
[firstTableView release];
[secondTableView release];
[super dealloc];
}
Затем реализуйте желаемые UITableViewDataSource
методы, например, так:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == firstTableView) {
// Do something
} else if (tableView == secondTableView) {
// Do something else
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (tableView == firstTableView) {
// Configure the cell
} else if (tableView == secondTableView) {
// Configure the cell a different way
}
}
Наконец, в didSelectRowAtIndexPath
:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == firstTableView) {
// Update something that is linked to the return values of secondTableView's dataSource methods
[secondTableView reloadData];
}
}
Этот подход гарантирует, что ваш контроллер автоматически получит права -viewDidLoad
, -viewWillAppear
и так далее. Помните, просто добавление представлений контроллера представления в качестве подпредставлений означает, что ваш контроллер не будет принимать эти вызовы.