1

I have one ViewController containing two TableViews as shown below.

enter image description here

So the top table (firstTable) currently has two records. And I want to populate the bottom table (secondTable) with a different set of data (data2) according to the selection at the top, which, in fact, happens in many of my Windows applications.

Initially, I wondered how I could have one viewcontroller as the data source host two tables? And vikingosegundo explains how in this topic. Great, I've got that part taken care of. Now, I have the following code from Manage1ViewController.

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// From Manage1ViewController.m for firstTable
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}

NSString *name0 = cell.textLabel.text;
NSString *tsecs0 = [self findTsecs:name0];

[self openCreateDB2];
NSMutableArray *items2;
items2 = [[NSMutableArray alloc] init];
NSString *sql = [NSString stringWithFormat:@"Select * From data2 WHERE tsec = '%i'", [tsecs0 integerValue]];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(connection2, [sql UTF8String], -1, &statement, Nil)==SQLITE_OK){
    while (sqlite3_step(statement)==SQLITE_ROW) {
        char *field1 = (char *) sqlite3_column_text(statement, 1);
        NSString *field1Str = [[NSString alloc]initWithUTF8String:field1];
        NSString *str1 = [[NSString alloc]initWithFormat:@"%@", field1Str];
        [items2 addObject:str1];
    }
    sqlite3_finalize(statement);
    sqlite3_close(connection2);
}

/* Start populating secondTable with items2

*/
}

So far, so good... And the application has data (items2) according to user's selection on firstTable that needs to be populated into secondTable. But how?

After reading several dozen topics here and there, I've learnt that there are a couple of ways by which you can pass data. Well, in this case, I can't use segue since I'm passing data within the same view control. Anyway, it seems that some people here suggest that I do it either with Delegate and NSNotification. And it looks like using a delegate is appropriate in my case. Again, how? Is it the same idea with use of a popover window where I think I've used a delegate?

I appreciate your advice.

Community
  • 1
  • 1
El Tomato
  • 6,479
  • 6
  • 46
  • 75

3 Answers3

3

You don't want to use either a delegate or a notification -- they're also used for passing information to other objects outside your controller. You need to implement the tableView:didSelectRowAtIndexPath: method of your first table to get the users selection. In that method, you would then get the data you need for the second table (based on the selection) and call reloadData on the second table.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Hmm... Then I have to figure out how to pass items2 to secondTable, which tells me that ManageViewController will now get involved. – El Tomato Jan 24 '13 at 17:40
  • Actually, ManageViewController is irrelevant, I guess. – El Tomato Jan 24 '13 at 17:43
  • @TBlue, you don't really "pass" the array to the second table. You just populate your table in cellForRowAtIndexPath using that array. – rdelmar Jan 24 '13 at 18:00
  • Okay. But Manage1ViewController does not have direct access to secondTable since that tableview belongs to ManageViewController, which manages multiple data sources. – El Tomato Jan 24 '13 at 18:56
  • @TBlue, Ok, I just noticed your answer. I'm not really sure what you're trying to do. Why are you setting the views of the two controllers to the table view? Aren't these two controllers just implementing the data source and delegate methods of the table views? There's no reason that they should be viewControllers at all -- they could just be subclasses of NSObject. In your question, you mention managing multiple tables with the same view controller. Why not have all the data source and delegate methods in one controller? – rdelmar Jan 24 '13 at 19:04
  • @TBlue, I just looked at vikingosegundo's post, and if you want to do it that way, then yes, a delegate is probably the way to go. You would put a delegate protocol in firstController, and in the didSelectRowAtIndexPath method, call that delegate method and pass whatever info you need in secondController (which would assign itself as the delegate) to do what you need to do there. – rdelmar Jan 24 '13 at 19:11
  • I have two separate viewcontrollers following a suggestion made by more experienced person than I am. If I can handle everything with one controller, that will be great except that I don't have a sample project that shows me how. – El Tomato Jan 24 '13 at 19:19
2

Call [self.tableView2 reloadData] after selecting a row in the first table view. Also you have to store required data outside this method for populated the second UITableView

Vitaly S.
  • 2,389
  • 26
  • 40
  • Okay. Thank you. But pardon me. tablewview2 (secondTable) belongs to a different class (Manage2ViewController) according to ManageViewController that manages different data sources. – El Tomato Jan 24 '13 at 17:19
  • My mistake. Actually if your table views are subviews of general view, you can handle them in the one view controller. As for me it's a good way if you want to share the same datasource between them. If you want to use different view controllers, store reference to the second in the first and pass data from the first to the second – Vitaly S. Jan 24 '13 at 17:22
  • Yeah, that's my initial question of how I pass items2 from Manage1ViewController to Manage2ViewController. – El Tomato Jan 24 '13 at 17:28
0

I have ManageViewController that manages two tableviews.

// ManageViewController.h //

#import <UIKit/UIKit.h>
#import "Manage1ViewController.h"
#import "Manage2ViewController.h"

@interface ManageViewController : UIViewController {
Manage1ViewController *firstController;
Manage2ViewController *secondController;
IBOutlet UITableView *firstTable;
IBOutlet UITableView *secondTable;
}
@end

// ManageViewController.m //

#import "ManageViewController.h"

@implementation ManageViewController

- (void)viewDidLoad {
[super viewDidLoad];
if (firstController == nil) {
    firstController = [[Manage1ViewController alloc] init];     
}
if (secondController == nil) {
    secondController = [[Manage2ViewController alloc] init];
}
[firstTable setDataSource:firstController];
[secondTable setDataSource:secondController];

[firstTable setDelegate:firstController];
[secondTable setDelegate:secondController];
firstController.view = firstController.tableView;
secondController.view = secondController.tableView;
}

@end
El Tomato
  • 6,479
  • 6
  • 46
  • 75
  • I have to admit that I didn't use a good key phrase. If I use 'Two TableViews' as opposed to 'Multiple TableViews,' I get a lot more useful hits. Silly me... – El Tomato Jan 24 '13 at 21:17