I'm trying to reload a UITableView
when the row of a different UITableView's
row is selected. 我遇到的问题之一是两者的UITableView's
观点相同。我想在 Table1 中进行选择以更改NSMutableArray
用于填充 Table2 的选项。然后重新加载表。
目前它工作正常,但只有当应用程序重新启动(或视图从堆栈中弹出然后重新访问)并viewWillAppear
再次被调用时
这是我的代码:
- (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];
}
我的UITableView
方法:
- (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;
}