I’m trying to reload a UITableView when the row of a different UITableView's row is selected. One of the problems I’m having is that both the UITableView's are in the same view. I would like a selection in Table1 to change the NSMutableArray that’s used to populate Table2. And then reload the table.
At the moment it works fine, but only when the app re-lauches (or the view is popped off the stack and then re-visited) and viewWillAppear is called again
Here’s my code:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:NO];
[self.navigationController setNavigationBarHidden:YES animated:NO];
self.navigationController.toolbarHidden = YES;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
selectedTableString = [prefs stringForKey:@"selectedTableString"];
if ([selectedTableString isEqualToString:@"Italy"]) {
jsonStringCountry = @"http://****/v1/public/cities?country=it";
}
else if ([selectedTableString isEqualToString:@"Spain"]) {
jsonStringCountry = @"http://****/v1/public/cities?country=es";
}
else if ([selectedTableString isEqualToString:@"UK"]) {
jsonStringCountry = @"http://****/v1/public/cities?country=uk";
}
else if ([selectedTableString isEqualToString:@"Brazil"]) {
jsonStringCountry = @"http://****/v1/public/cities?country=br";
}
NSLog(@"from two t selectedTableString %@",selectedTableString);
// Download the yoodeal JSON
NSString *jsonString = [NSString stringWithContentsOfURL:[NSURL URLWithString:jsonStringCountry] encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding error:nil];
NSLog(@"jsonStringCountry is %@", jsonStringCountry);
NSMutableArray *itemsTMP = [[NSMutableArray alloc] init];
// Create parser for the yoodeal api
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [parser objectWithString:jsonString error:nil];
itemsTMP = [results objectForKey:@"results"];
self.displayItems = [itemsTMP copy];
}
My UITableView methods:
- (int)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [displayItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}
// Get item from tableData
NSDictionary *item = (NSDictionary *)[displayItems objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:@"name"];
[cell.textLabel setFont:[UIFont fontWithName: @"Asap-Bold" size: 14.0f]];
return cell;
}
You’ll need to use the delegate method of
UITablveView. The method is:This methods gets called when you click on the row of
UITableView. Use this in following manner: