gpt4 book ai didi

iphone - 如何将数据从详细 View Controller 传回 uitableview?

转载 作者:太空狗 更新时间:2023-10-30 03:42:00 28 4
gpt4 key购买 nike

在我的应用程序中,我使用 uitable 从我的列表中选择类别。我的任务是,当用户单击或选择一个单元格时,他应该能够在下一个 View (详细 View )中查看所选单元格的详细信息。当他在详细 View 中选择该项目时,他应该能够在 TableView 中移回并且应该能够在 rootivew Controller 中看到所选项目。

我能够从 tableview 正确导航到详细 View ,但我无法将在详细 View 中选择的项目显示到 rootviewcontroller。

请帮我解决这个问题。

enter image description here图一是我的 Root View Controller 页面。例如:如果用户选择@"make",他将能够看到所有与@"make"相关的类别.在下一页(哪个图像 2)。

enter image description here 图片是我的详情页。

当用户选择@"abarth"时,它应该显示在 Root View Controller 页面(第一页)中。

以下是我的 Root View Controller 页面代码:

- (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",@"$2,500",
@"$5,000",
@"$7,500",
@"$10,000",
@"$15,000",
@"$20,000",
@"$25,000",
@"$30,000",
@"$35,000",
@"$40,000",
@"$45,000",
@"$50,000",
@"$60,000",
@"$70,000",
@"$80,000",
@"$90,000",
@"$100,000",
@"$500,000",
@"$1,000,000",nil];

}
else if ([self.title isEqualToString:@"Price Max"])
{
detail = [[NSArray alloc]initWithObjects:@"Max",
@"$2,500",
@"$5,000",
@"$7,500",
@"$10,000",
@"$15,000",
@"$20,000",
@"$25,000",
@"$30,000",
@"$35,000",
@"$40,000",
@"$45,000",
@"$50,000",
@"$60,000",
@"$70,000",
@"$80,000",
@"$90,000",
@"$100,000",
@"$500,000",
@"$1,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];

}

最佳答案

您需要使用自定义委托(delegate)。在您的详细 View 中创建一个协议(protocol)并在您的 Root View 中实现它。将选定的字符串作为参数传递给委托(delegate)方法,并从委托(delegate)方法中将其显示在您的文本字段中。

//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
}

关于iphone - 如何将数据从详细 View Controller 传回 uitableview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10578281/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com