无法通过didSelectRowAtIndexPath传递值

时间:2016-04-25 14:13:22

标签: ios objective-c uitableview didselectrowatindexpath

我有一个表格视图。单击tableview单元格时,我会抓取特定的textlabel值。在APIRequest.m文件中,我有一个变量shipmentReferenceNo,其值im在didSelectRowAtIndexPath上指定为textlabel值。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    tabBar = [self.storyboard instantiateViewControllerWithIdentifier:@"TabBarController"];
    [self.navigationController pushViewController:tabBar animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *label = (UILabel *)[cell viewWithTag:123123];
    self.request = [[APIRequest alloc]init];

    self.request.shipmentReferenceNo = label.text;

    NSLog(@"hello %@",  label.text);

}

当我试图从APIRequest.m文件中按如下方式访问shipmentReferenceNo时,如下所示,其为空

NSLog(@"sd %@",self.shipmentReferenceNo);

虽然我先前在didSelectRowAtIndexPath中分配了这个空值,但可能是什么原因?

1 个答案:

答案 0 :(得分:1)

试试此代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
 {
   [tableView deselectRowAtIndexPath:indexPath animated:YES];
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   UILabel *label = (UILabel *)[cell viewWithTag:123];
   self.request = [[APIRequest alloc]init];
   self.request.shipmentReferenceNo = label.text;
   NSLog(@"hello %@",  label.text);

  dispatch_async(dispatch_get_main_queue(), ^{
  tabBar = [self.storyboard instantiateViewControllerWithIdentifier:@"TabBarController"];
  [self.navigationController pushViewController:tabBar animated:YES];
 });

}

也在表格视图数据源中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   UITableViewCell *cell = ;
  [cell.label setTag:123];
}

这样可行但我建议通过将dispatch_async代码移动到单独的方法来使代码更加组织。