简体   繁体   English

如何将数据从tableview传递到另一个tableview单元?

[英]how to pass data from tableview to another tableview cell?

I am using static UITableView to show profile of a customer. 我正在使用静态UITableView来显示客户的个人资料。

It has SelectCountryCell to select country which, when clicked, opens new UITableView with country list. 它具有SelectCountryCell来选择国家/地区,单击该国家/地区后,会打开带有国家/地区列表的新UITableView

When I select country from Country TableView, It should display in previous TableView's SelectCountryCell. 当我从“国家/地区” TableView中选择国家/地区时,它应显示在以前的TableView的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. 您应该将静态单元格绑定到UITableViewController插座,并将配置文件同步方法放入- viewWillAppear方法。

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. 之后,当用户使用静态内容返回到UITableViewController实例时,配置文件数据将自动更新。

You can define a delegate in your CityTableView and then define a method in this delegate. 您可以在CityTableView中定义一个委托,然后在此委托中定义一个方法。

You should realize this method in your CountryTableView. 您应该在CountryTableView中实现此方法。

Then you may get what you want. 然后,您可能会得到想要的东西。

I only offer you a thought. 我只给你一个想法。 You should find the detail by yourself. 您应该自己找到细节。

MasterViewController.h 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 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 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 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];
}

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

相关问题 如何将数据从 TableView 单元格传递到 Swift 5 中的另一个 viercontroller? - How to pass data from TableView cell to another viercontroller in Swift 5? 当在单元格中按下按钮时,如何将数据从一个 Tableview 传递到另一个 Tableview? - how to pass data from one Tableview to another Tableview when button is pressed in cell? 如何将 TableView 的回调传递给 TableView Cell? - How to pass callback from TableView to TableView Cell? 将数据从 tableview 传递到另一个 tableview - pass data from tableview to another tableview 如何将数据从视图控制器传递到另一个表视图? - How to pass data from a viewcontroller to another tableview? 如何将单元格中单击的按钮上的数据传递到另一个表格视图? - How to pass data on button clicked in cell to another tableview? 从 tableView 单元格传递数据被点击到第二个 tableView - Pass data from tableView cell is tap to second tableView 如何将数据从Firebase在tableview单元之间传递到视图控制器? - how to pass data from firebase between tableview cell to view controllers? 将数据从单元格中的按钮传递到另一个表格视图? - passing data from button in cell to another tableview? TableView数据使用相同的UIVIewController传递到另一个tableview中? - TableView data pass into another tableview with same UIVIewController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM