[英]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.