xcode 如何将数据从详细视图控制器传回 uitableview?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10578281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass data back from detail view controller to uitableview?
提问by Nilesh .S. Joshi
In my app i making use of uitable to select category from my list. my task is,when ever user click or select a cell he should be able to view the selected cell detail in next view(detail view). and when he select the item in a detail view he should be able to move back in a table view and should be able to see the selected item in a rootivew controller.
在我的应用程序中,我使用 uitable 从我的列表中选择类别。我的任务是,当用户单击或选择一个单元格时,他应该能够在下一个视图(详细信息视图)中查看所选单元格的详细信息。并且当他在详细视图中选择项目时,他应该能够在表格视图中移回并应该能够在 rootivew 控制器中看到所选项目。
i am able to navigate properly from tableview to detail view,but i am not able to show the item which is selected in detail view to rootviewcontroller.
我能够从 tableview 正确导航到详细信息视图,但是我无法将在详细信息视图中选择的项目显示到 rootviewcontroller。
please help me out with this issue.
请帮我解决这个问题。
image one is my rootview controller page.
for example : if user select @"make" he will able to see all the releated category of @"make"
. in a next page(which image2).
图一是我的 rootview 控制器页面。例如:如果用户选择@"make",他将能够看到@"make" 的所有相关类别。在下一页(其中 image2)。
image to is my detail page.
image to 是我的详细信息页面。
and when user select @"abarth" it should be display in a rootview controller page(which is page one).
并且当用户选择 @"abarth" 时,它应该显示在 rootview 控制器页面(这是第一页)中。
following is my code of rootview controller page:
以下是我的 rootview 控制器页面的代码:
- (void)viewDidLoad
{
self.car = [[NSArray alloc]initWithObjects:@"Make",@"Model",@"Price Min",@"Price Max",@"State",nil];
[super viewDidLoad];
}
-(NSInteger) numberOfSectionInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.car count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *TextCellIdentifier = @"Cell";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:TextCellIdentifier];
if (cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TextCellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [self.car objectAtIndex:[indexPath row]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (0 == indexPath.row)
{
NSLog(@"0");
self.detailcontroller.title = @"Make";
}
else if (1 == indexPath.row)
{
NSLog(@"1");
self.detailcontroller.title = @"Model";
}
else if (2 == indexPath.row)
{
NSLog(@"2");
self.detailcontroller.title = @"Price Min";
}
else if (3 == indexPath.row)
{
self.detailcontroller.title = @"Price Max";
}
else if (4 == indexPath.row)
{
NSLog(@"3");
self.detailcontroller.title = @"State";
}
[self.navigationController
pushViewController:self.detailcontroller
animated:YES];
}
following is my detail view page code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([self.title isEqualToString:@"Make"])
{
detail = [[NSArray alloc]initWithObjects:@"Any Make",@"Abarth",@"AC",@"ADAYER",@"Adelaide",@"ALFA ROMEO",@"ALLARD",@"ALPINE-RENAULT",@"ALVIS",@"ARMSTRONG",
@"ASTON MARTIN",@"AUDI",@"AUSTIN",@"AUSTIN HEALEY",@"Barossa",@"BEDFORD",
@"BENTLEY",@"BERTONE",@"BMW",@"BOLWELL",@"BRISTOL",@"BUICK",@"BULLET",
@"CADILLAC",@"CATERHAM",@"CHERY",@"CHEVROLET",@"CHRYSLER",@"CITROEN",
@"Country Central",@"CSV",@"CUSTOM",@"DAEWOO",@"DAIHATSU",@"DAIMLER",
@"DATSUN",@"DE TOMASO",@"DELOREAN",@"DODGE",@"ELFIN",@"ESSEX",
@"EUNOS",@"EXCALIBUR",@"FERRARI",nil];
if ([self.title isEqualToString:@"Abarth"])
{
detail = [[NSArray alloc]initWithObjects:@"HI", nil];
}
}
else if ([self.title isEqualToString:@"Model"])
{
detail = [[NSArray alloc]initWithObjects:@"Any Model", nil];
}
else if ([self.title isEqualToString:@"Price Min"])
{
detail = [[NSArray alloc]initWithObjects:@"Min",@",500",
@",000",
@",500",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@"0,000",
@"0,000",
@",000,000",nil];
}
else if ([self.title isEqualToString:@"Price Max"])
{
detail = [[NSArray alloc]initWithObjects:@"Max",
@",500",
@",000",
@",500",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@",000",
@"0,000",
@"0,000",
@",000,000",nil];
}
else if ([self.title isEqualToString:@"State"])
{
detail = [[NSArray alloc]initWithObjects:@"Any State",
@"Australian Capital Territory",
@"New South Wales",
@"Northern Territory",
@"Queensland",
@"South Australia",
@"Tasmania",
@"Victoria",
@"Western Australia",nil];
}
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [detail count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [detail objectAtIndex:
[indexPath row]];
cell.font = [UIFont systemFontOfSize:14.0];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController popViewControllerAnimated:YES];
}
回答by zahreelay
You need to make use of custom delegates. Create a protocol in your detailview and implement it in your rootview.Pass the selected string as parameter to delegate method and from the delegate method, display it in your textfield.
您需要使用自定义委托。在您的 detailview 中创建一个协议并在您的 rootview 中实现它。将选定的字符串作为参数传递给委托方法,并从委托方法中将其显示在您的文本字段中。
//something like this
@interface detailViewController
//?protocol declaration
@protocol myDelegate
@optional
-(void)selectedValueIs:(NSString *)value;
// set it as the property
@property (nonatomic, assign) id<myDelegate> selectedValueDelegate;
// in your implementation class synthesize it and call the delegate method
@implementation detailViewController
@synthesize selectedValueDelegate
// in your didselectRowAtIndex method call this delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self selectedValueDelegate])selectedValueIs:valueString] ;
[self.navigationController popViewControllerAnimated:YES];
}
@end
// In your rootViewController conform to this protocol and then set the delegate
detailViewCtrlObj.selectedValueDelegate=self;
//Implement the delegate Method
-(void)selectedValueIs:(NSString *)value{
{
// do whatever you want with the value string
}
回答by Abhishek Singh
Hi you will have to do it using protocols and delegate Please see my linkon protocol and delegate
嗨,您必须使用协议和委托来完成请参阅我关于协议和委托的链接
you can also do it by creating a variable in appdelegate , setting its property and accessing it in any other view .
您还可以通过在 appdelegate 中创建一个变量、设置其属性并在任何其他视图中访问它来实现。
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.yourVariable;