简体   繁体   中英

how to pass data from tableview to another tableview cell?

I am using static UITableView to show profile of a customer.

It has SelectCountryCell to select country which, when clicked, opens new UITableView with country list.

When I select country from Country TableView, It should display in previous TableView's SelectCountryCell.

How do I do this?

You should bind the static cells to outlets in your UITableViewController and put the profile syncing methods to the - viewWillAppear method.

When the user changes the country in the Country list the profile updates. After that when the user moves back to the UITableViewController instance with static content the profile data will be automatically updated.

You can define a delegate in your CityTableView and then define a method in this delegate.

You should realize this method in your CountryTableView.

Then you may get what you want.

I only offer you a thought. You should find the detail by yourself.

MasterViewController.h

#import "DetailViewController.h"

@interface MasterViewController : UITableViewController <DetailProtocol> // Note this.

@property (strong, nonatomic) DetailViewController *detailViewController;
@property (strong, nonatomic, readwrite) NSString *selectedCountry;

@end

MasterViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.detailViewController) {
        self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    }
    self.detailViewController.delegate = self; // THIS IS IMPORTANT...
    [self.navigationController pushViewController:self.detailViewController animated:YES];
}

// Note this -- It's a delegate method implementation
- (void)setCountry:(NSString *)country
{
    self.selectedCountry = country;
}

DetailViewController.h

@protocol DetailProtocol <NSObject>
-(void)setCountry:(NSString *)country;
@end

@interface DetailViewController : UIViewController

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (unsafe_unretained) id <DetailProtocol> delegate; // Note this

@end

DetailViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.delegate setCountry:[countries objectAtIndex:indexPath.row]]; // Note this
    [self.navigationController popViewControllerAnimated:YES];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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