简体   繁体   English

选择其他uitable行的行时如何重新加载uitable

[英]How to reload a uitable when the row of a different uitable's row is selected

I'm trying to reload a UITableView when the row of a different UITableView's row is selected. 当选择其他UITableView's行的行时,我尝试重新加载UITableView One of the problems I'm having is that both the UITableView's are in the same view. 我遇到的问题之一是两个UITableView's都在同一视图中。 I would like a selection in Table1 to change the NSMutableArray that's used to populate Table2. 我希望在Table1中进行选择以更改用于填充Table2的NSMutableArray 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 目前,它工作正常,但仅当应用程序重新启动(或视图从堆栈弹出然后重新访问)并且再次调用viewWillAppear

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: 我的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;
}

为表格视图创建出口

 [self.yourTableView reloadData];

Use [tablename reloadData]; 使用[tablename reloadData]; onclick. onclick。

When you are updating tableview contents, it will not refresh the view. 当您更新tableview内容时,它不会刷新视图。 You can only see changes once the view controller is reloaded. 重新加载视图控制器后,您才能看到更改。 For updating the table instantly you need to call the above code 为了立即更新表,您需要调用上面的代码

您应该创建UITableView的属性secondTableView ,然后使用[secondTableView reloadData]

You'll need to use the delegate method of UITablveView . 您将需要使用UITablveView的委托方法。 The method is: 方法是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

This methods gets called when you click on the row of UITableView . 当您单击UITableView的行时,将调用此方法。 Use this in following manner: 通过以下方式使用它:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   if(tableView == Table1)
   {
        //do the changes in array you want to do.
        [table2 reloadData];
   }
   else
   {
        //do the changes in array you want to do.
        [table1 reloadData];
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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