加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 如何将数据从详细视图控制器传递回uitableview?

发布时间:2020-12-14 19:45:43 所属栏目:百科 来源:网络整理
导读:在我的应用程序中,我使用uitable从我的列表中选择类别. 我的任务是,当用户点击或选择一个单元格时,他应该能够在下一个视图(详细视图)中查看所选的单元格细节.当他在详细视图中选择项目时,他应该能够在表格视图中向后移动,并且应该能够在rootivew控制器中查看
在我的应用程序中,我使用uitable从我的列表中选择类别.
我的任务是,当用户点击或选择一个单元格时,他应该能够在下一个视图(详细视图)中查看所选的单元格细节.当他在详细视图中选择项目时,他应该能够在表格视图中向后移动,并且应该能够在rootivew控制器中查看所选项目.

我能够从tableview正确导航到详细视图,但我无法在rootviewcontroller的详细视图中显示所选的项目.

请帮我解决这个问题.

图像一是我的rootview控制器页面.
例如:如果用户选择@“make”,他将能够看到@“make”的所有相关类别
.在下一页(哪个image2).

?image to是我的详细页面.

当用户选择@“abarth”时,它应该显示在rootview控制器页面(第一页)中.

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

    }
    else if ([self.title isEqualToString:@"Price Max"])
    {
        detail = [[NSArray alloc]initWithObjects:@"Max",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];

}

解决方法

您需要使用自定义委托.在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
       }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读