Pergunta

So I have a string called cellNumString that I created in the .h of my DetailViewController. In my HistoryTableViewController.m I have a prepareForSegue method in which I set the string to anything. For now I have this:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"Push"]) {

        HistoryDetailsViewController *detailVC = (HistoryDetailsViewController*)segue.destinationViewController;
        [detailVC setCellNumString:@" cell number"];
    }
}

All I want is the @"cell number" part to be changed to cell.textlabel.text. But I can't use cell. How would I be able to get the value of cell.textlabel.text and use it as my string that I pass to the next view?

All help is appreciated, thanks.

Foi útil?

Solução

If the segue is on the cell, you can use this answer https://stackoverflow.com/a/13989915/1835155

You can get also get the selected cell using:

NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedIndexPath];
NSLog(@"%@", cell.textLabel.text);

Outras dicas

Assuming you want to set the string to a cell that is selected or you know the cell whose text label you want to access you could do the following:

//If you want the selected cell    
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
//If you know the cell row index e.g. 1, 2, 3...
NSIndexPath *path = [NSIndexPath indexPathWithIndex:yourRow];
//Create the cell at the index path
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path]
//Do stuff with cell...

Pass the selected cell as the sender parameter to performSegueWithIdentifier:sender:, like so:

[self performSegueWithIdentifier:@"showPromoViewController" sender:cell];

Then in prepareForSegue:sender:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"Push"]) {

        HistoryDetailsViewController *detailVC = (HistoryDetailsViewController*)segue.destinationViewController;

        // Get the value of cell.textlabel.text
        UITableViewCell *cell = (UITableViewCell *)sender;
        NSString *text = cell.textLabel.text;

        // Pass it to the destination view controller
        [detailVC setCellNumString:text];
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top