Search code examples
iosuitableviewdidselectrowatindexpath

Grabbing the value of specific textlabel and making json in didSelectRowAtIndexPath


Following is my cellForRowAtIndexPath method in MyOrdersController.m file...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = nil;
    cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@""];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:nil];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    self.shipmentReferenceNumberTextLabel = [[UILabel alloc]init];

    self.shipmentReferenceNumberTextLabel.text = amountArray[indexPath.row];
    self.shipmentReferenceNumberTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];


    tableView.tableFooterView = [UIView new];

    cell.layer.borderWidth = 1.0;
    cell.layer.cornerRadius = 10;
    cell.layer.borderColor = [UIColor blackColor].CGColor;

    [cell.contentView addSubview:self.productAmountLabel];
    [cell.contentView addSubview:self.productAmountTextLabel];

    return cell;
}

I have got a textlabel shipmentReferenceNumberTextLabel in it. I have to grab the value of shipmentReferenceNumber when a user clicks on a particular cell and I have to make json request with it in a file called APIRequest.m. Im trying grab its value as follows,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    tabBar = [self.storyboard instantiateViewControllerWithIdentifier:@"TabBarController"];
    [self.navigationController pushViewController:tabBar animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MyOrdersController *detailVC = [[MyOrdersController alloc]init];
    NSLog(@"%@",  detailVC.shipmentReferenceNumberTextLabel.text);

}

The problem is that, its printing null while im trying to grab its value. How can I sort out this issue.


Solution

  • You can get value of Lable from an array.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
    {
          NSLog(@"%@",amountArray[indexPath.row]);
    }
    

    You will get selected row's label value.