【问题标题】:Pass data from Table View Cell to a View Controller in IOS将数据从 Table View Cell 传递到 IOS 中的 View Controller
【发布时间】:2017-07-25 11:16:37
【问题描述】:

我知道这个问题被问了很多次,我搜索了很多并尝试了很多解决方案但没有奏效。我制作了一个自定义表格视图,其中数据是从服务加载的。数据加载非常有限,当用户单击单元格时,我必须将数据的详细信息显示到新的视图控制器中。它应该传递携带数据的各个单元的数据。我尝试了 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];

    }
}

【问题讨论】:

  • 这个prepareForSegue方法被调用了吗?并且满足isEqualToString:@"ShowDetail"这个条件?
  • yes 方法被调用,它移动到新的 vc 但我传递的值不会去,因为我有 NSLo 一些值,但它在它们前面显示 null。 @Mr.Bista
  • 在分配给它之前注意你正在打印 phon 变量...
  • 在这里查看我的答案stackoverflow.com/questions/45004596/…
  • @HamzaImran 为什么要为每个属性使用一个数组,更好的方法是使用模型类来保留所有这些属性,也比传递一组属性更容易传递一个对象

标签: ios objective-c uitableview


【解决方案1】:

这个问题的问题是您没有正确填充 _Price 和其他数组,因此在您填充 _Title 数组的地方,填充其他数组以及 _Price、_City

【讨论】:

    【解决方案2】:

    出口不会实例化,因为出口是一个变量(或属性)。

    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];
         ....
         ....
         ....
         }
    }
    

    【讨论】:

    • destViewController.titesStr = [_Title objectAtIndex:indexPath.row];它显示的错误是 titesStr 不在 ShowView Controller 中。 @luckyShubhra
    • 检查这一行 [self performSegueWithIdentifier:@"ShowDetail" sender:self];
    • 发件人应该是自己。
    • 你的 self.tableView 是全局声明的吗??
    • ** destViewController.titesStr = [_Title objectAtIndex:indexPath.row];它显示的错误是 titesStr 不在 ShowView Controller ** 因为为了从 ShowViewController 访问 titesStr 您需要在 ShowViewController 的接口文件中创建 titesStr 的属性,即 .h 文件。
    【解决方案3】:

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

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

    【讨论】:

    • 不使用索引路径进行segue是什么意思? @KKRocks
    • 你在提问时得到了正确的索引路径吗?
    • 您询问单元格索引路径? @KKRocks
    • 是的,使用这个代码 NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    • 是的,它是正确的,当我单击任何单元格时,它会移动到 vc。 @KKRocks
    猜你喜欢
    • 2020-02-17
    • 1970-01-01
    • 2019-07-26
    • 2019-06-15
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多