将表视图单元格中的数据传递到IOS中的视图控制器

时间:2017-07-25 11:16:38

标签: ios objective-c uitableview

我知道很多时候会问这个问题,我经常搜索并尝试了许多解决方案,但没有成功。我已经创建了一个自定义表视图,其中从服务加载数据。数据负载非常有限,我必须在用户单击一个单元格时向新视图控制器显示数据的详细信息。它应该传递携带数据的相应小区的数据。我已经尝试过segue技术将数据传递给新的vc但是失败了,它在我传递的值中显示为null。我在新的vc中创建了一些标签,我在其中调用表视图单元格中的值。我的代码是,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"ShowDetail"]) {
        //Do something
        ShowViewController *destViewController = segue.destinationViewController;
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];


         destViewController.tites = [_Title objectAtIndex:indexPath.row];
         destViewController.prce = [_Price objectAtIndex:indexPath.row];
         NSLog(@"PPPP is %@",destViewController.phon);
         destViewController.ara = [_LandArea objectAtIndex:indexPath.row];
         destViewController.phon = [_Phone objectAtIndex:indexPath.row];
         destViewController.citi = [_City objectAtIndex:indexPath.row];
         destViewController.loc = [_location objectAtIndex:indexPath.row];
         destViewController.tye = [_type objectAtIndex:indexPath.row];
         destViewController.Idss = [_Id objectAtIndex:indexPath.row];
         destViewController.nam = [_name objectAtIndex:indexPath.row];
         destViewController.emal = [_email objectAtIndex:indexPath.row];
         destViewController.roomss = [_rooms objectAtIndex:indexPath.row];
         destViewController.wash = [_washroom objectAtIndex:indexPath.row];
         destViewController.flloors = [_floor objectAtIndex:indexPath.row];
         destViewController.stat = [_status objectAtIndex:indexPath.row];
         destViewController.descrp = [_descrip objectAtIndex:indexPath.row];
         destViewController.countryy = [_country objectAtIndex:indexPath.row];

    }
}

3 个答案:

答案 0 :(得分:1)

这个问题的问题是你没有正确填充_Price和其他数组,所以在填充_Title数组的地方,填写其他数组,如_Price,_City

答案 1 :(得分:0)

在您的代码中进行以下更改:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"ShowDetail" sender:indexPath];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
 {
     if ([segue.identifier isEqualToString:@"ShowDetail"])
      {
    ShowViewController *destViewController =(ShowViewController *) segue.destinationViewController;
           NSIndexPath *indexPath = (NSIndexPath *)sender;
           destViewController.tites = [_Title objectAtIndex:indexPath.row];
           destViewController.prce = [_Price objectAtIndex:indexPath.row];
           // use this indexpath for segue
      }
  }

答案 2 :(得分:0)

插座不会实例化,因为插座是变量(或属性)。

在加载该nib时,实例化nib中的对象,然后在创建对象之后但在将awakeFromNib发送到所有相关对象之前,将它们尽可能地分配给每个出口。

在您的情况下,您可以在ShowViewController中传递数据并更新ShowViewController的viewDidLoad或viewDidAppear中的标签。

将ShowViewController接口中的字符串定义为

@property (strong, nonatomic) NSString * titesStr;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     if ([segue.identifier isEqualToString:@"ShowDetail"]) {
     //Do something
     ShowViewController *destViewController = segue.destinationViewController;
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
     destViewController.titesStr = [_Title objectAtIndex:indexPath.row];
     ....
     ....
     ....
     }
}

在ShowViewController中,viewDidAppear将标签更新为

- (void)viewDidAppear:(BOOL)animated
 {
      [super viewDidAppear:animated];
      tites.text = titesStr;
 } 

代替逐个传递数据,您也可以使用数组。最佳实现是使用模型类。

编辑

[self performSegueWithIdentifier:@"ShowDetail" sender:tableView];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     if ([segue.identifier isEqualToString:@"ShowDetail"]) {
     //Do something
     UITableView *tv = (UITableView*)sender;
     ShowViewController *destViewController = segue.destinationViewController;
     NSIndexPath *indexPath = [tv indexPathForSelectedRow];
     destViewController.tites = [_Title objectAtIndex:indexPath.row];
     ....
     ....
     ....
     }
}