-1

I have six labels in my table view controller that all have the same id, however they have different tags. Once I click a cell, I would like to pass the value of each label (float values) into the next view controller activity. I am not sure how to go about this, and what to declare in the .h file and the .m file to make it work. Here is the code from the .m tableviewcontroller file.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NodeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             CellIdentifier forIndexPath:indexPath];

    UILabel *label;
    Node *node = [self.nodes objectAtIndex:indexPath.row];
    label = (UILabel *)[cell viewWithTag:1];
    label.text = node.nodeid;
    label = (UILabel *)[cell viewWithTag:2];
    label.text = [NSString stringWithFormat:@" %.0f", node.lastdelivered1.floatValue];
    label = (UILabel *)[cell viewWithTag:3];
    label.text = [NSString stringWithFormat:@" %.0f", node.lastdelivered2.floatValue];
    label = (UILabel *)[cell viewWithTag:4];
    label.text = [NSString stringWithFormat:@" %.0f", node.lastdelivered3.floatValue];
    label = (UILabel *)[cell viewWithTag:5];
    label.text = [NSString stringWithFormat:@" %.0f", node.lastdelivered4.floatValue];
    label = (UILabel *)[cell viewWithTag:6];
    label.text = [NSString stringWithFormat:@" %.0f", node.lastdelivered5.floatValue];
    label = (UILabel *)[cell viewWithTag:7];
    label.text = [NSString stringWithFormat:@" %.0f", node.lastdelivered6.floatValue];
    return cell;
}
meda
  • 45,103
  • 14
  • 92
  • 122
Akhil Yeleswar
  • 87
  • 1
  • 1
  • 12
  • 1
    What have you *tried* so far? There is **plenty** of pre-existing literature out there to help you try and solve this problem. – esqew Jul 10 '14 at 19:34
  • I have tried creating a new instance of the labels, but it didnt work – Akhil Yeleswar Jul 10 '14 at 19:37
  • for anyone wanting to answer the question, Im trying to pass data that specifically lies in the table cell into the view controller, that is what I am having trouble with – Akhil Yeleswar Jul 10 '14 at 19:48
  • First of all, you must call `viewWithTag:` method on the `contentView` of your cell, because your label is subView of contentView, but not of cell. So, call `[cell.contentView viewWithTag:...];` instead of `cell viewWithTag:...];` – arturdev Jul 10 '14 at 19:50
  • ok thanks, so can I call that method in the uiviewcontroller which I am trying to pass the data to – Akhil Yeleswar Jul 10 '14 at 20:00
  • @AkhilYeleswarapu nice of you to accuse me of stealing code when all I m trying to do is help you out – meda Jul 11 '14 at 22:08
  • @meda look man Im sorry I had a rough morning, and once I sent it to you you didnt respond for an hour and that has happened before where the person took it and didnt help. Im very sorry and I apologize, I was only quick to accuse because I don't know you. But Id still love to get help if you accept the apology – Akhil Yeleswar Jul 11 '14 at 22:56
  • @meda I hope you can accept the apology, since this is the internet I was suspicious but it was nothing personal. I was just worried since you didnt respond for a while – Akhil Yeleswar Jul 11 '14 at 23:00
  • @meda were you able to work on it yet? – Akhil Yeleswar Jul 12 '14 at 18:38
  • @meda I thought you were going to help... – Akhil Yeleswar Jul 16 '14 at 21:58
  • @AkhilYeleswarapu I am, the problem is I could not run the project, I got multiple errors. and project files and reskit missing. I wish you gave a little sample project with just that tableView Piece. – meda Jul 16 '14 at 21:59

2 Answers2

0

in .m tableviewcontroller:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Controller *myController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"yourViewControllerIdentifer"];
    myController.node = [self.nodes objectAtIndex:indexPath.row];
    [self presentViewController:myController animated:YES completion:Nil];
    //or [self.navigationController pushViewController:myController animated:YES]; if Navigation Controller use
}

in .h file your controller that receive node

@interface Controller : UIViewController
@property (strong, nonatomic) Node *node;
@end
0

Try to save all labels text in to NSArray *labelsArr, and in header of you next ViewController add new property

@property (nonatomic, strong) NSArray *savedArr;

then in you didSelectRowAtIndexPath transmit you arr to other

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Controller *myController = [[Controller alloc] init];
    myController.savedArr = labelsArr;
    // here call you VC ass you want;
}

but if you use - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender i advice you to do myController.savedArr = labelsArr; there.

Don't forget to put you labels text from array to place where you want in viewDidLoad, or viewWillAppear.

such way you can pass text to other labels, but ass for me using array is better.

  • instead of that, I am getting the text from each label and converting it into an integer. From there, how can I pass the integer to the next viewcontroller – Akhil Yeleswar Jul 10 '14 at 21:43
  • @AkhilYeleswarapu save to array int value, [NSArray arrayWithObjects:[label.text intValue], [label2.text intValue],..., nil]; –  Jul 11 '14 at 10:32